#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;
}
option to make ofstream constantly flush?
how can I get ofstream to flush after every stream?
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.
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.
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.
ok thanks again snk_kid.
Just as I posted this I found this
//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.
is there any way I can get this into the constructor?
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; \\1while (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); \\1ostr << " _ 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?
Use std::endl instead of \n. This is the equivalent of placing a \n in the stream followed by a call to flush.
stdout <
stdout <
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement