-
Updating a file
i have a problem with updating a file
first the program read the file then save it in memory after that sort the records and ask user to input which record the user wants to update
after find it and update the record
how can i put it back in the same position in the file?
after open and read the file first time then close it
i open file again to put the updated record back in the same positon i use this
f=fopen("a:\\Country.txt","r+");
then write it back
fwrite(&cty,sizeof(cty),1,f);
it seems like it doesn't save it back in the same position it was
do i have to use fseek? how to use it?
Thanks
-
You could use fseek(), you just need to calculate the number of bytes to offset before you start writing. This would be the sizeof the struct, against the number of structs before the one you want to write.
Or you could rewrite the complete file (providing you have it all in memory).
I see a small problem with your code, you are using "r+" to open the file. If you are going to write structs with fwrite (and read with fread), you should use a binary mode.