问题
I'm trying to create a program that reads text from a file that was previously created, inverts it, and writes to another file. The code I have now allows me to invert single words, but stops whenever there is a space or a paragraph. I could use some help in overcoming this problem. This is the code I have so far:
#include <stdio.h>
#include <string.h>
int main()
{
int c, d;
FILE *fich1, *fich2;
char string [100], *inicio, *fim, tempo;
fich1 = fopen("FicheiroInicial.txt", "rt");
if (fich1 == NULL)
{
printf ("Erro - Nao foi possivel abrir o ficheiro\n");
return (-1);
}
fscanf (fich1, "%s", string);
fclose (fich1);
inicio = string;
fim = inicio + strlen(string) - 1;
while (fim > inicio)
{
tempo = *inicio;
*inicio = *fim;
*fim = tempo;
++inicio;
--fim;
}
fich2 = fopen("FicheiroFinal.txt", "wt");
fprintf (fich2, "%s", string);
fclose (fich2);
printf ("%s", string);
return 0;
}
Thanks in advance!
回答1:
I believe fscanf() will only give you a word at a time. you will need to iterate over it until it returns EOF (end of file)
来源:https://stackoverflow.com/questions/20032262/reverse-string-c