Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ofstream::flush() return ostream?

Tags:

c++

file

ofstream

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?

like image 231
SwiftMango Avatar asked Feb 22 '14 07:02

SwiftMango


People also ask

What does Ostream flush do?

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.

How do I flush a file in C++?

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.

How do you flush a stream in C++?

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.

What is flush function in ostream library?

C++ Ostream Library - flush. Description. It is used to flush output stream buffer and synchronizes the associated stream buffer with its controlled output sequence.

How do you synchronize a stream buffer with its controlled 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.

What happens when basic_ostream is set?

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.


1 Answers

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).

like image 174
Collin Dauphinee Avatar answered Oct 24 '22 04:10

Collin Dauphinee