0
myfile<<hashdugumu[key].numara;

I have this piece of code.For example,i would like to write to eighth line.How do i do that in c++ ?Thanks in advance.

user6207597
  • 13
  • 1
  • 3
  • 1
    http://stackoverflow.com/questions/16423538/how-to-write-on-a-specific-line-in-a-file?rq=1 – Jonathan Potter May 12 '16 at 20:15
  • Is the line you want to write exactly the same length as the line already existing there? Then just count seven lines (using e.g. `std::getline`) and then write the text. If the lines are *not* exactly the same length you can't do that, instead you have to use another file or memory as temporary storage. – Some programmer dude May 12 '16 at 20:22
  • yes they are the same length – user6207597 May 12 '16 at 20:23
  • http://stackoverflow.com/questions/30642827/how-to-write-to-middle-of-a-file-in-c – stark May 12 '16 at 21:19

1 Answers1

0

If the line you want to write is exactly the same length (in bytes, not in characters, remember some encodings (like e.g. UTF-8) is variable length) then it's very easy: Just skip over the first seven lines and then write the line.

There is a caveat with this though: input streams and output streams have different stream positions. So if you read from a combined input/output file stream then only the read position will change, so if you just try to write directly then you will not write at the same position. To solve this you need to get the read position, and set the write position to the same value.


As an alternative, or if the data you want to write is not the same size as the existing data, then you have to use a temporary "buffer", be it another file or an actual in-memory buffer.

If the file is not big you can use an in-memory buffer, for example using a std::vector for the lines. Read each line into the vector, and then modify the lines (elements in the vector) that you want to modify. Finally reopen the file for writing, truncating it, and then just write each "line" to the file.

There is a slight problem with the above though when it comes to the rewriting of the data, and that is if the file is truncated and then there's an error when you write to the file, you can lose data. This can be dsolved by using a temporary file.

Using a temporary file it's easier to not bother with the in-memory buffer, and instead read from the original file and write directly to the temporary file. Knowing when you should write something else is done by keeping track of the current line numbers, which is easy if you read one line at a time. In your example you read the first seven lines from the original file and write them to the temporary file, after the seventh line you write your special eight line while skipping the original eight line from the original file, and then just continue reading/writing the remaining lines. When done close the files and then rename the temporary file as the original file.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Writing to a file truncates the file to that length. OP wants to write to the middle of a file, which is not so simple. – stark May 12 '16 at 21:19
  • @stark [`std::fstream`](http://en.cppreference.com/w/cpp/io/basic_fstream) by default uses `ios::out | ios::in` as the mode when opening, and according to [this reference](http://en.cppreference.com/w/cpp/io/basic_filebuf/open) it is the same as the [`fopen`](http://en.cppreference.com/w/cpp/io/c/fopen) mode `"r+"` which opens an *existing* file for reading and writing, no truncating is being done and the file *must* exist. – Some programmer dude May 13 '16 at 05:53