Originally Posted by
SuchtyTV
Should not the read start at the beginning. I do not see the problem?
C requires a file positioning function to be used between output and input on a read/write stream, even if it's a no-op like fseek(file, 0, SEEK_CUR).
Code:
#include <stdio.h>
int main() {
char buf[1000];
FILE *file = fopen("foo.txt", "a+");
fprintf(file, "This was a triumph.\n");
fseek(file, 0, SEEK_SET);
buf[fread(buf, 1, sizeof(buf), file)] = '\0';
printf("%s-----\n", buf);
// append mode automatically positions to the end before writing
fprintf(file, "A second line.\n");
fseek(file, 0, SEEK_SET);
buf[fread(buf, 1, sizeof(buf), file)] = '\0';
printf("%s-----\n", buf);
fclose(file);
return 0;
}