This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream ifile("hello.txt");
std::ofstream ofile("hello_copy.txt");
char temp[81];
// 忽略文件对象判断的过程
do
ifile.getline(temp, 81);
if (ifile.rdstate() == std::ios::goodbit) // 正常结束
ofile << temp << '\n';
else if (ifile.rdstate() == std::ios::failbit) // 读取长度超限,进入fail状态
ifile.clear(); // 恢复流状态
ofile << temp; // 注意没有换行
}
else // 文件读取结束,进入fail+eof状态
ofile << temp;
break;
} while (1);
ifile.close(); // 如没有这两个函数析构函数也可关闭,但建议显式书写
ofile.close();
return 0;