If I run the following code, no file is created at all:
std::ofstream outputFile(strOutputLocation.c_str(), std::ios::binary);
outputFile.write((const char*)lpResLock, dwSizeRes);
outputFile.close();
However, if I add a flush() before the close, it works:
std::ofstream outputFile(strOutputLocation.c_str(), std::ios::binary);
outputFile.write((const char*)lpResLock, dwSizeRes);
outputFile.flush();
outputFile.close();
Does the standard library actually require this, or is it a bug in the Visual C++ CRT?
It's a bug. Reading §27.8.1.10/4, abridged:
What does
rdbuf()->close()
do? According to §27.8.1.3/6, abridged, emphasis mine:That is, it's suppose to flush. (Indeed, the call to
flush()
ultimately does the same thing.)Note the call to
close()
itself isn't needed, as the destructor ofbasic_ofstream
will callclose()
.Are you checking the file before you exit from the program? OS will buffer all IO, so may not see any data(unless you flush) in the file before you exit.