其他分享
首页 > 其他分享> > C语言 FILE写文件中途断开磁盘驱动器测试(2)

C语言 FILE写文件中途断开磁盘驱动器测试(2)

作者:互联网

note

结论

测试代码

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;
}

输出结果

标签:文件,当写,FILE,ret,C语言,中途,磁盘驱动器,关闭
来源: https://www.cnblogs.com/pandamohist/p/16528232.html