I was trying to do:
std::ofstream outFile (fname, std::ios::binary);
//...
outFile.flush();
outFile.close();
which works fine. But when I try to combine the two lines since flush returns a reference:
outFile.flush().close();
It gives error saying:
error: ‘struct std::basic_ostream<char>’ has no member named ‘close’
Then I looked at the reference more carefully and found out it actually returns ostream
instread of ofstream
..
why is this the case? Is it a bug or designed?
std::ostream::flushSynchronizes the associated stream buffer with its controlled output sequence. For stream buffer objects that implement intermediate buffers, this function requests all characters to be written to the controlled sequence.
C++ File I/O Flushing a stream You can do this either directly by invoking the flush() method or through the std::flush stream manipulator: std::ofstream os("foo. txt"); os << "Hello World!" << std::flush; char data[3] = "Foo"; os. write(data, 3); os.
In C++, we can explicitly be flushed to force the buffer to be written. Generally, the std::endl function works the same by inserting a new-line character and flushes the stream. stdout/cout is line-buffered that is the output doesn't get sent to the OS until you write a newline or explicitly flush the buffer.
C++ Ostream Library - flush. Description. It is used to flush output stream buffer and synchronizes the associated stream buffer with its controlled output sequence.
Synchronizes the associated stream buffer with its controlled output sequence. For stream buffer objects that implement intermediate buffers, this function requests all characters to be written to the controlled sequence. Internally, the function accesses the output sequence by first constructing a sentry object.
The basic_ostream object ( *this ). May be set if the construction of sentry failed. The synchronization operation failed (including if the function catches an exception thrown by an internal operation). When set, the integrity of the stream may have been affected. Multiple flags may be set by a single operation.
This is by design.
ostream
is a class that encompasses writing a sequence to an associated stream buffer, which is an abstract class that is in charge of writing and reading to something. That something could be anything; a file, the console, a socket, etc.
ofstream
is really just a convenience class that extends an ostream
to 'automatically' use a type of stream buffer that writes to a file, called a filebuf
. The only functions provided by ofstream
are open
, is_open
, and close
; these are really just an easy interface to the file-specific functionality of the underlying filebuf
stream buffer object.
If you really wanted, you could create an ostream
that does the exact same thing as an ofstream
. It just requires more code and isn't as pretty or explicit.
std::filebuf f;
f.open(fname, std::ios::binary);
std::ostream os(&f);
os.write("Hello world!", 12);
os.flush();
// Error! close() isn't part of the streambuf interface, it's specific to
// files.
os.close();
Basically, ofstream
doesn't actually do any sort of writing, so it doesn't implement functions like flush
and write
. All of the writing is done via the ostream
class, which manipulates an underlying streambuf
class, whose job it is to make sure the data reaches it's intended location (i.e. a file).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With