This is by design.
ostream is a class that includes writing a sequence to the associated stream buffer, which is an abstract class that is responsible for writing and reading something. It could be anything; file, console, socket, etc.
ofstream is really just a convenience class that extends ostream to 'automatically' using the type of stream buffer that writes to a file called filebuf . The only functions provided ofstream are open , is_open and close ; this is actually a simple interface for file-specific functions of the base object of the filebuf stream filebuf .
If you really want to, you can create an ostream that does the same thing as ofstream . It just requires more code and is not so pretty or explicit.
std::filebuf f; f.open(fname, std::ios::binary); std::ostream os(&f); os.write("Hello world!", 12); os.flush();
Basically, ofstream does not actually ofstream , so it does not implement functions like flush and write . All entries are made using the ostream class, which manages the base streambuf class, whose task is to make sure that the data has reached its intended location (i.e. the file).
source share