I'm looking for some clarification on how seekg()
and seekp()
works with respect to when you are writing to a file. Say for instance I had a file like so:
offset 0: 2
offset 4: 4
offset 8: 6
offset 12: 8
offset 16: 10
Now I want to open the file and do some seeks to read and write values.
fstream file;
file.open("file.txt", fstream::in |fstream::out | fstream::binary);
file.seekp(0, ios::end) // seek to the end of the file
int eofOffset = file.tellp(); // store the offset of the end-of-file, in this case 20
int key = 0;
file.seekg(12, ios::beg); // set the seek cursor to offset 12 from the beginning of the file
file.read((char *) &key, (int)sizeof(int)); // read in the next 4 bytes and assign it to key, in this case 8
file.seekg(8, ios::beg); // set the seek cursor to offset 8 from the beginning of the file
file.read((char *) &key, (int)sizeof(int)); // read in the next 4 bytes and assign it to key, in this case 6
Now I want to write to the end of the file. Since the seekg()
function only moves the seek cursor, my seekp()
cursor should still be at the end of the file right? So:
int newKey = 12;
file.write((char *) &newKey, sizeof(int));
should make my file now look like:
offset 0: 2
offset 4: 4
offset 8: 6
offset 12: 8
offset 16: 10
offset 20: 12
Now what happens to my file if I choose to seek to an offset and write its value as the offset to the value that was just inserted. For example, I want offset 8
to hold eofOffset = 20
since we just inserted 12 at that offset.
If I do:
file.seekp(8, ios::beg);
file.write((char *) &eofOffset, sizeof(int));
does it correctly rewrite my file to look like this:
offset 0: 2
offset 4: 4
offset 8: 20
offset 12: 8
offset 16: 10
offset 20: 12
Please let me know if I am making any errors using the seekg()
and seekp()
functions.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…