-
Editing a text file
Hi,
I just started learning C++ not long ago, and has been stucked at this problem for quite sometimes.
I have this text file which keep the records of cars.
00001 Toyota Camry
00002 Nissan Sunny
00003 Honda Accord
...
and so on
The problem is when i use ofstream class and try to make a modification to the line that i want to do a lazy deletion.
00002 Nissan Sunny -> xxxxxxxxxxxxxx (to indicate that this record is not a valid record)
00001 Toyota Camry
xxxxxxxxxxxxxxxxxxx
00003 Honda Accord
...
and so on
I ended up with file that has only the modification line and the rest was erased.
xxxxxxxxxxxxxxxxxx
I am using a fixed length record so i do know which byte to start the modification and where to stop.
Code:
ofstream ofile;
ofile.open("test.txt, ios::out);
ofile.seekp(20);
ofile.write("xxxxxxxxxxxxxx", 10 :confused: );
ofile.close();
this is something that similar to the code that exists in my program.
in summary: how do i modify a part of a text file without erasing the rest of the content. Thank you in advance, I cannot proceed with the homework if i cant solve this confusion. :confused:
-
ofile.open("test.txt, ios::out | ios::ate);
-
Your original code opened your file for write, truncuating the file (making its size zero):
Code:
ofile.open("test.txt, ios::out);
AD's code will open the file for append, which doesn't erase the file.
-
Thanks a million guys. Really appreciate ur help