Detect write errors in LVoc.

Write failures in LVoc::Write() were going unnoticed.  If disk space
runs out, the output file might get truncated without any indication
of failure.
This commit is contained in:
Jeroen 2016-03-07 18:55:18 +01:00
parent c6314d927d
commit ae5ad8a49e

View File

@ -58,7 +58,14 @@ public:
void Write(const std::string& fname) const {
std::ofstream out(fname.c_str());
// Little-known fact: ofstream tracks failures but does not, by default,
// report them. You have to tell it to, or check for errors yourself.
out.exceptions(std::ifstream::failbit | std::ifstream::badbit);
Write(out);
// Make sure the file is flushed, so that any errors are reported. If we
// flush implicitly in the destructor, it won't be able to throw
// exceptions.
out.close();
}
void Write(std::ostream& out) const {
for(int i=data.size()-1; i>=0; --i)