Advertisement
tutorfree

ED - leitura e exibição de linhas de txt

Jun 29th, 2016
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #ifdef WIN32
  5.     #include <conio.h>
  6.     #define LIMPA_TELA system("cls")
  7. #else
  8.     #include <ncurses.h>
  9.     #define LIMPA_TELA system("clear")
  10. #endif
  11.  
  12. int main ()
  13. {
  14.     char c[41];
  15.     FILE *arq = fopen("entrada.txt", "rt"); //abre o arquivo no leitura de texto
  16.  
  17.     if (arq == NULL)
  18.     {
  19.         printf("Não foi possível abrir o arquivo.\n");
  20.         system("pause");
  21.         exit(1);
  22.     }
  23.  
  24.     while(fgets(c, 40, arq)!= NULL) //char[41]-1 para acomodar o '\0' no 2o parâmetro
  25.     {
  26.         printf("%s",c); // ou puts(c);
  27.     }
  28.     fclose(arq); //fecha o arquivo
  29.     printf("\n\n");
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement