Ich recherchiere über Eingabe/Ausgabe-Datei.Below Code beziehen sich auf einige Funktionen wie: fgetc(), fgets(), fputs(). Ich weiß nicht, warum es nicht genau so funktioniert, wie ich es will. Vielen Dank! Unten ist mein Code:Warum der Eingang "abc !!!" aber die Ausgabe ist nicht "abC+++"?
#include <stdio.h>
int main()
{
FILE *fp; //FILE type pointer
int c; //using to get each character from file
char buffer [256]; //array as buffer to archive string
fp = fopen("file.txt", "r"); /*open a file with only read mode*/
if(fp == NULL)
{
perror("Error in opening file");
return(-1);
}
while(!feof(fp)) /*check if has not yet reached to end of file*/
{
c = getc (fp); //get a character from fp
if(c == '!')
{
ungetc ('+', fp); //replace '!' by '+'
}
else
{
ungetc(c, fp); //no change
}
fgets(buffer,255,fp);//push string of fp to buffer
fputs(buffer, stdout); //outputting string from buffer to stdout
}
return(0);
}
Für Tipps, wie man eine Leseschleife macht siehe http://stackoverflow.com/questions/5431941/why-is-which-feof-file-always-wrong –