问题描述
我有一个很大的对象要序列化到磁盘,像这样:
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;
}
engine->serialize(OutEngineStream);
OutEngineStream.close();
std::cout << "\"" << EngineFile << "\" successfully wrote to disk." << std::endl;
}
问题是,somtimes serialize
需要的磁盘空间比可用磁盘空间大.例如只有30M的可用存储空间,但serialize
需要200M的存储空间.在这种情况下,我通常可以open
流.在serialize
期间一切正常,而close
不返回任何内容.该程序运行良好,但是磁盘上只有30M文件.
The problem is, somtimes serialize
requires larger disk space than available. e.g. there is only 30M storage available but serialize
requires 200M. In this case I can normally open
the stream. During serialize
everything goes well, and close
returns nothing. The program runs well, but there is only a 30M file on the disk.
如何了解此案?
推荐答案
首先,serialize
应不断验证其写操作是否成功,如果失败则抛出.
First of all, serialize
should constantly verify whether its write operations succeed and throw should they fail.
在您提供的代码中,您应该检查 OutEngineStream.fail()
(在调用close
之前,它涵盖的情况比bad
更多)(因为close
可能还会设置此状态).但是,这仍然会导致serialize
实施不正确.
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.
这篇关于文件写入期间磁盘已满.我如何获得有关此的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!