Hi team,
This is doing my head in...and I'm sure i'm SO close to solving this! I need some guided help on how I can modify my code below, so that I can append some additional text to the beginning of a text file, which already has content within it.
Basically, what this overwrites the contents of the file and just adds "This is just some random text."...Code:#include <stdio.h> #include <stdlib.h> int main(void) { char str[80] = "This is just some random text.\n"; FILE *fp; char *p; int i; /* open file3.txt for output */ if((fp = fopen("file3.txt", "w"))==NULL) { printf("Cannot open file.\n"); exit(1); } /* write str to disk */ p = str; while(*p) { if(fputc(*p, fp)==EOF) { printf("Error writing file.\n"); exit(1); } p++; } fclose(fp); /* open file3.txt for input */ if((fp = fopen("file3.txt", "r"))==NULL) { printf("Cannot open file.\n"); exit(1); } /* read back the file */ for(;;) { i = fgetc(fp); if(i == EOF) break; putchar(i); } fclose(fp); return 0; }
Would REALLY welcome any of you to show me the proper code!
Cheers,
K