Wesley Ranger Wesley Ranger - 3 months ago 23
C++ Question

Disk gets full during file write. How can I get notified about this?

I have a quite big object to serialize to disk, like this:

if (!EngineFile.empty())
std::ofstream OutEngineStream(EngineFile);
if (!OutEngineStream)
std::cerr << "Failed to write to file \"" << EngineFile << "\"! Aborting ..." << std::endl;
return -1;

std::cout << "\"" << EngineFile << "\" successfully wrote to disk." << std::endl;

The problem is, somtimes
requires larger disk space than available. e.g. there is only 30M storage available but
requires 200M. In this case I can normally
the stream. During
everything goes well, and
returns nothing. The program runs well, but there is only a 30M file on the disk.

How can I get to know about this case?


First of all, serialize should constantly verify whether its write operations succeed and throw should they fail.

In the code you've presented you should check OutEngineStream.fail() (it covers more cases than bad) before calling close (because close may also set this state). This however will still leave serialize implemented incorrectly.