Advertisement
tutorfree

ED - Abertura e leitura de arquivo

Jun 29th, 2016
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.69 KB | None | 0 0
  1. // exercicio de ED
  2. // Abertura e leitura do n0 de linhas de um arquivo
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #ifdef WIN32
  8.     #include <conio.h>
  9.     #define LIMPA_TELA system("cls")
  10. #else
  11.     #include <ncurses.h>
  12.     #define LIMPA_TELA system("clear")
  13. #endif
  14.  
  15. int main()
  16. {
  17.     int nl=0;
  18.     char c;
  19.     FILE *arq;
  20.     arq=fopen("entrada.txt", "rt"); //abre o arquivo
  21.  
  22.     if (arq==NULL)
  23.     {
  24.         printf("Não foi possível abrir o arquivo.\n");
  25.         exit(1);
  26.     }
  27.     while(fscanf(arq,"%c",&c)==1)
  28.     {
  29.         if(c=='\n') nl++;
  30.     }
  31.     fclose(arq);//fecha o arquivo
  32.     printf("Numero de linhas = %d\n", nl); /* exibe o resultado na tela */
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement