I want to make modifications to the middle of a text file using c++, without altering the rest of the file. How can I do that?
-
2This is not a duplicate. The previous post was only about how to read a record, this is about writing to a file. You need to read more carefully. – neuromancer Mar 27 '10 at 17:57
-
1Your elaboration that the replacement is the same length makes the meat of the question how to reposition the file pointer, which is exactly what the answers to your other question provided. – Ben Voigt Mar 27 '10 at 18:33
3 Answers
Use std::fstream.
The simpler std::ofstream would not work. It would truncate your file (unless you use option std::ios_base::app, which is not what you want anyway).
std::fstream s(my_file_path); // use option std::ios_base::binary if necessary
s.seekp(position_of_data_to_overwrite, std::ios_base::beg);
s.write(my_data, size_of_data_to_overwrite);

- 7,244
- 3
- 31
- 39
-
1If using the `std::ios_base::binary` flag, don't forget that you'll need to or this with `std::ios_base::out` – qbert220 May 21 '15 at 10:55
-
6Using `std::ios_base::binary | std::ios_base::out` mode flags causes truncation after the data written to the file. Using `std::ios_base::binary | std::ios_base::out | std::ios_base::in` prevents this. – qbert220 May 21 '15 at 11:07
-
So, do I **have** to open the file for reading AND writing to modify part of the file, even if I never actually *read* from it? Is it possible to modify (not append) with **only** write access without overwriting everything? Perhaps fseek itself requires the file to be open for reading, even if fread is not used? (Though this seems odd to me) Finally, is this an OS limitation, or just how the standard C++ library works? – Dexter Feb 11 '17 at 18:52
-
@qbert220 your comment is so valuable, it is very useful for me. I had the same issue that whenever I tried to write a trunk of data into a file, it replaced the whole file, causing me to scratch my head until I have found your comment. Thanks mate – N.T.C Jul 18 '21 at 23:59
If the replacement string is the same length, you can make the change in place. If the replacement string is shorter, you may be able to pad it with zero-width spaces or similar to make it the same number of bytes, and make the change in place. If the replacement string is longer, there just isn't enough room unless you first move all remaining data.

- 277,958
- 43
- 419
- 720
-
1That sounds good, but what's the code for that? Let's assume that the string is the same length. – neuromancer Mar 27 '10 at 18:19
-
3Well, it depends on which I/O library you're using, the C standard library, C++ standard library, or an OS-specific API. But generally there are three steps. When opening the file, specify write access without truncation, e.g. fopen(fname, "r+"). Seek to the desired location with e.g. fseek. Write the new string with e.g. fwrite or fprintf. In some APIs, like Windows OVERLAPPED or linux aio, you can say "write to this location" without a separate seek step. – Ben Voigt Mar 27 '10 at 18:30
-
fseek doesn't work with ofstream. How can it be done with ofstream? – neuromancer Mar 27 '10 at 18:49
-
I tried seekp and when I wrote to the file it overwrote everything. – neuromancer Mar 27 '10 at 19:07
-
3Ok, I'll emphasize part of my previous comment "when opening the file, specify write access WITHOUT TRUNCATION". From http://stdcxx.apache.org/doc/stdlibug/30-3.html,
For output file streams the open mode out is equivalent to out|trunc, that is, you can omit the trunc flag
. So use a bidirectional fstream instead of an ofstream. – Ben Voigt Mar 27 '10 at 19:26 -
2Glad you finally got there. In the future you'll get an answer quicker and with a lot less frustration if you tell us: What libraries, what compiler and whether you want it portable, what you've tried so far, the behavior you're seeing now, and what you wanted. For this question, it would have helped to say "I'm trying to overwrite just part of a file using ofstream. Here's the code (you actually did put that in another question). The new data shows up in the file, but no matter what I do the previous contents disappear. How can I make edits while keeping the other parts of the file." – Ben Voigt Mar 27 '10 at 20:09
Generally, open the file for reading in text mode, read line after line until the place you want to change, while reading the lines, write them in a second text file you opened for writing. At the place for change, write to the second file the new data. Then continue the read/write of the file to its end.

- 7,723
- 7
- 59
- 122