我一次将内容写入 csv 文件,想知道在刷新操作过程中写入是否会失败
我的目的是了解写入文件可能被破坏的不同方式
假设以下是 csv 文件写入成功后的结构
1,1001210212
2,8941321654
6,845646
17,564968896
假设将第 3 行刷新到文件时发生错误,是否会导致以下形式
1,1001210212\n //showing \n just for understanding purpose. Not actually visible as "\n" in the file
2,8941321654\n
6,845
或者
1,1001210212\n
2,8941321654\n
或者
1,1001210212\n
2,8941321654\n
6,845646\n //Fails right after writing the line. Not sure if such an error is even possible
或者
1,1001210212\n
2,8941321654\n
6,845646
如果需要,我的代码
东西.cpp
hash_map<int, unsigned long long> tableData;
//assume hash map is populated here
string outFile = "C:\outFile.csv";
FILE *fout = NULL;
if (outFile.length())
{
fout = fopen(outFile.c_str(), "a");
}
if (fout != NULL)
{
for (vector<uint32_t>::iterator keyValue = keys.begin();
keyValue != keys.end(); ++keyValue)
{
fprintf(fout,"%d,",*keyValue);
fprintf(fout,"%lld\n",tableData[*keyValue]);
fflush(fout);
}
}