note
- 本次测试基于前文
- 本次测试内容: 当写文件中途, 取下U盘后, 此时, 执行关闭文件, 观察能否正确关闭文件
结论
- 当写文件中途, 取下U盘后, 此时, 执行关闭文件, 能正确关闭文件流。
测试代码
void call_write(const std::string& file_name)
{
FILE* pfile = nullptr;
errno_t ret = fopen_s(&pfile, file_name.c_str(), "w");
if (0 != ret)
{
std::cout << "\n 11111 ret=" << ret << "\n";
return ;
}
const char parr[]{ "1234567890\n" };
const size_t len = strlen(parr);
for (int index = 0; index < 10; ++index)
{
if (len != fwrite(parr, 1, len, pfile))
{
std::cout << "22222, index << " << index << ", error_id=" << ferror(pfile) << "\n";
break;
}
else
{
std::cout << "index=" << index << " succeeded to call fwrite, ";
}
int ret = fflush(pfile);
std::cout << ", fflush_ret =" << ret << "\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
}
int closeResult = fclose(pfile);
std::cout << "\n ====================333333==== close_ret=" << closeResult << "\n\n";
pfile = nullptr;
}
输出结果