Jump to content
  • Advertisement
Sign in to follow this  
Funkymunky

std::fstream output problems

This topic is 4166 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having strange issues with using std::fstream for output. If the file doesn't exist, it won't be created unless I open it for specifically "std::ios::out" (not "std::ios::in|std::ios::out", the default). Even then it won't output to the file until I close it and reopen it for in|out. If I want to add a line at the end of the file (opened with just default in|out), I try to call myFile.seekp(std::ios::end) and then try to do myFile << "string". Nothing gets outputted at the end of the file.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Funkymunky
I'm having strange issues with using std::fstream for output.

If the file doesn't exist, it won't be created unless I open it for specifically "std::ios::out" (not "std::ios::in|std::ios::out", the default).


That is the correct behaviour. Files are not created when you open them for input (or for both input and output), because otherwise, you wouldn't have a way to check for their existence (you do this by trying to opening them for input and seeing if this fails). Automatically creating files for output is convenient, but automatically creating them for input is nonsensical - what data would be read in?

But don't specify std::fstream and then fiddle with the mode flags. Instead, use the derived class std::ofstream. This is open for std::ios::out (and not in) by default.

Quote:
Even then it won't output to the file until I close it and reopen it for in|out.


It outputs to the file when it closes. Reopening is only needed so that you can verify this from within your program. ;) File I/O is buffered (just like console I/O), so output to disk only actually happens when the stream is "flushed". There are a few ways to do this; closing the stream implies a flush.

Letting the object go out of scope (or 'delete'ing a stream dynamically allocated with 'new' - although there is probably no reason for doing that) implicitly closes the file, which in turn implicitly flushes as well. But you don't see contents in the file until the end of the stream's scope unless you explicitly flush in some way. (You can also output 'std::flush' to the file; and just as with console I/O, 'std::cout' implies a combination of a newline and a flush.)

Quote:
If I want to add a line at the end of the file (opened with just default in|out), I try to call myFile.seekp(std::ios::end) and then try to do myFile << "string". Nothing gets outputted at the end of the file.


Because std::ios::end is some numeric value (the type is std::ios_base::seekdir, but this is just a typedef, probably to some integer type), and the one-argument form of seekp() interprets that value as a number of bytes to seek forward from the beginning of the file (the expected argument type is std::streampos, but again this is presumably an integer typedef - the two integer types may differ, but an implicit conversion exists). You want the two-argument form: i.e., you can't say "go to (the end)" directly, but instead "go to (0) bytes away from (the end)". RTFM.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!