If I want to read from and write to a file (in order to, say, write a spellchecker that automatically corrects any mistakes it detects)can I open it twice with two separate pointers, once in read mode and once in append mode? If not, what is the proper way to modify a file while reading it?
I apologize for the neophyte question.
Name:
Anonymous2012-03-08 17:20
copy the file to a temporary location and use it for reading. Linux use /tmp/whatev, Windows... lol.
FILE *f = fopen(filename, "a+");
fpos_t beginning, end;
fgetpos(f, &end); /* use this for writing */
rewind(f);
fgetpos(f, &beginning); /* use this for reading */
Since you said "append mode" you can do this and jump between positions with fgetpos and fsetpos. If you want to modify the file "in place" in Standard C you'd either have to read the original file into memory and reopen it for writing or write to a temporary file and then rename() it to the same name as the original after deleting the original.