Created
May 9, 2011 03:08
-
-
Save carlosbrando/961991 to your computer and use it in GitHub Desktop.
Grava e mostra arquivo usando fgets(), feof(), fflush() e rewind().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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