Skip to content

Instantly share code, notes, and snippets.

@carlosbrando
Created May 9, 2011 03:08
Show Gist options
  • Save carlosbrando/961991 to your computer and use it in GitHub Desktop.
Save carlosbrando/961991 to your computer and use it in GitHub Desktop.
Grava e mostra arquivo usando fgets(), feof(), fflush() e rewind().
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main(int argc, char const *argv[]) {
char str[80];
FILE *fp;
if ((fp = fopen("TESTE", "w+")) == NULL) {
printf("O arquivo não pode ser aberto.\n");
exit(1);
}
do {
printf("Digite uma string (CR para sair):\n");
gets(str);
strcat(str, "\n"); /* Acrescenta uma nova linha */
fputs(str, fp);
} while(*str != '\n');
fflush(fp);
/* Agora lê e mostra o arquivo */
rewind(fp); /* reinicializa o indicador de posição de arquivo
* para o começo do arquivo */
while (!feof(fp)) {
fgets(str, 79, fp);
printf("%s", str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment