Howto update text files
-
I'm trying to update a text file by overwritting a part of the text in the middle of the file. I'm using the fwrite function. I manage to overwrite something unfortunately the new text overlaps the text of the contiguous line when it is longer than previous text. For example: My first line Line to override My next line Gives: My first line Overriden line which is longer ext line I wonder if it is possible to insert longer than previous line text with fwrite and if so how to do, if not how to do ;) I don't have the code here but it is something like the folowing (assuming you are reading line 2 only):
fFile = fopen("myfile.txt", "r+"); ... fgets(pBuffer)) { ftell(); fWrite("new text"); fflush(fFile) ... } fclose(fFile);
Sorry for the code, if you want more I can post it later. Yarp http://www.senosoft.com/ -
I'm trying to update a text file by overwritting a part of the text in the middle of the file. I'm using the fwrite function. I manage to overwrite something unfortunately the new text overlaps the text of the contiguous line when it is longer than previous text. For example: My first line Line to override My next line Gives: My first line Overriden line which is longer ext line I wonder if it is possible to insert longer than previous line text with fwrite and if so how to do, if not how to do ;) I don't have the code here but it is something like the folowing (assuming you are reading line 2 only):
fFile = fopen("myfile.txt", "r+"); ... fgets(pBuffer)) { ftell(); fWrite("new text"); fflush(fFile) ... } fclose(fFile);
Sorry for the code, if you want more I can post it later. Yarp http://www.senosoft.com/No, you cant do that way, you have to read the whole file into a buffer, make the change in the buffer and then write to the file. for safetly you write it to a temp file and if the write succeeds then rename it to the original file name ofcource delete the first one. or if you are not too bother about data in the file and would like to take a change, then write it to the same file.
-prakash