Jump to content
  • Advertisement
Sign in to follow this  
Genjix

option to make ofstream constantly flush?

This topic is 4725 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

how can I get ofstream to flush after every stream?
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main(int argc , char **argv)
{
	ofstream stdout("output");
	stdout << "some text\n" << "some more text\n" << "yet more text\n";

	int *p = NULL;
	*p = 10;

	stdout << "never gets streamed\n";

	return 0;
}

the resulting file (output) is 0 bytes? how can I get it to stream to the file until the segmentation fault without constantly flushing it (doing it manually)? thanks for your time.

Share this post


Link to post
Share on other sites
Advertisement
file streams have sub-types of basic_streambuf which typically preform buffered I/O.

Either you invoke std::basic_ostream::flush/std::basic_streambuf::pubsync to force flushing at specific point or create a new sub-type of std::basic_streambuf to do unbuffered I/O then associate an instance of it with any C++ I/O stream you like.

Share this post


Link to post
Share on other sites
ok thanks again snk_kid.

Just as I posted this I found this

Quote:

2.10.2 Implicit Synchronization Using the unitbuf Format Flag

You can achieve a kind of automatic synchronization for output files by using the format flag ios_base::unitbuf. It causes an output stream to flush its buffer after each output operation as follows:


ofstream ostr("/tmp/fil");
ifstream istr("/tmp/fil");

ostr << unitbuf; \\1

while (some_condition)
{ ostr << "_ some output_"; \\2
// process the output
istr >> s;
// _
}



//1 Set the unitbuf format flag.
//2 After each insertion into the shared file /tmp/fil, the buffer is automatically flushed, and the output is available to other streams that read from the same file.
Since it is not overly efficient to flush after every single token that is inserted, you might consider switching off the unitbuf flag for a lengthy output that is not supposed to be read partially.

ostr.unsetf(ios_base::unitbuf); \\1
ostr << " _ some lengthy and complicated output _";
ostr.flush().setf(ios_base::unitbuf); \\2



Quote:

//1 Switch off the unitbuf flag. Alternatively, using manipulators, you can say ostr << nounitbuf;
//2 Flush the buffer and switch on the unitbuf flag again. Alternatively, you can say ostr << flush << unitbuf;


is there any way I can get this into the constructor?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Use std::endl instead of \n. This is the equivalent of placing a \n in the stream followed by a call to flush.

stdout <

Share this post


Link to post
Share on other sites
Quote:
Original post by Genjix
Just as I posted this I found this


Ah yes completely forget about that.

Quote:
Original post by Genjix
is there any way I can get this into the constructor?


Looking at the I/O stream & locales book there doesn't appear to be a way of setting formatting flags in constructors for any stream type.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!