[filesystem] Problem mixing read and write on io fstream
I have a program which opens a boost fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware of. filename is a wpath. f.open(filename,ios::in | ios::out | ios::binary | ios::trunc) The program flow is 1) write some data 2) read the data 3) overwrite old data //this fails I am aware that mixing reading and writting is complicated due to buffering and as far as I know the solution is to do a seekg or seekp when changing between reading and writting, I am doing this between 1, 2 and 3. In 'my' program, writting in step 3 fails. I wrote a simple program that does just the above 3 steps with seeks and it works but in my program, it sets the failbit. After lots of debugging in internal fstream code I figured that an internal flag _IOREAD gets set somehow in step 2 which indicates that the stream is now readonly and causes subsequent writes to fail. It also seems that _IOREAD is set anytime any read operation is done on a read+write file, is this correct? And now how do I 'unset' this flag? I tried calling f.clear and f.seekp but that doesnt helps. What might be special in my program that causes this read-mode flag to remain set while this doesn't happens in a barebone/simple test program. I am not doing anything except f.seekg, f.get, f.read. Sachin Garg ps. If it matters, I am using visual studio 9.0. Problem might not be boost specific as boost only uses stl fstream but any help will be really useful.
On Aug 22, 2008, at 9:09 AM, Sachin Garg wrote:
I have a program which opens a boost fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware f. filename is a wpath.
<snip> You most likely just need to reset the state of the stream. Once you reach EOF the corresponding flag is set accordingly and thus any further read/write operation will fail. Add a f.clear(); between the read and write statements and you should be fine. You may also need/want to adjust the position marker of the stream by using 'seekg()' accordingly. Ciao, Andreas
I have something like your problem...
First I have a question, when, you say :
1) write some data
2) read the data
3) overwrite old data //this fails
In step 3, does it mean you replace ponctualy some datas int the stream ?
Or do you replace the whole datas ?
In my case, I'm making a http resume... I what to seek to the end of the
stream, but by me :
making a seekp(sizeOfFile) erase the beginning of my file and replace it by
0 (zero) ....
Why ?
Is that not possible to go further the EOF ?
2008/8/22 Sachin Garg
I have a program which opens a boost fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware of. filename is a wpath.
f.open(filename,ios::in | ios::out | ios::binary | ios::trunc)
The program flow is
1) write some data 2) read the data 3) overwrite old data //this fails
I am aware that mixing reading and writting is complicated due to buffering and as far as I know the solution is to do a seekg or seekp when changing between reading and writting, I am doing this between 1, 2 and 3.
In 'my' program, writting in step 3 fails. I wrote a simple program that does just the above 3 steps with seeks and it works but in my program, it sets the failbit.
After lots of debugging in internal fstream code I figured that an internal flag _IOREAD gets set somehow in step 2 which indicates that the stream is now readonly and causes subsequent writes to fail. It also seems that _IOREAD is set anytime any read operation is done on a read+write file, is this correct? And now how do I 'unset' this flag? I tried calling f.clear and f.seekp but that doesnt helps.
What might be special in my program that causes this read-mode flag to remain set while this doesn't happens in a barebone/simple test program. I am not doing anything except f.seekg, f.get, f.read.
Sachin Garg
ps. If it matters, I am using visual studio 9.0. Problem might not be boost specific as boost only uses stl fstream but any help will be really useful. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Andreas Masur
-
Germain BARRET
-
Sachin Garg