加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何检测C中的空文件?

发布时间:2020-12-16 10:10:28 所属栏目:百科 来源:网络整理
导读:参见英文答案 Checking for an empty file in C++ ????????????????????????????????????8个 我正在尝试使用eof和peek,但两者似乎都没有给我正确的答案. if (inputFile.fail()) //check for file open failure{ cout "Error opening file" endl; cout "Note
参见英文答案 > Checking for an empty file in C++ ????????????????????????????????????8个
我正在尝试使用eof和peek,但两者似乎都没有给我正确的答案.

if (inputFile.fail()) //check for file open failure
{
    cout << "Error opening file" << endl;
    cout << "Note that the program will halt" << endl;//error prompt
}

else if (inputFile.eof())
{
    cout << "File is empty" << endl;
    cout << "Note that program will halt" << endl; // error prompt
}
else
{
    //run the file
}

它使用此方法无法检测到任何空文件.如果我使用inputFile.peek而不是eof,它会将我的好文件作为空文件.

解决方法

请使用以下方式查看

if ( inputFile.peek() == std::ifstream::traits_type::eof() )
{
   // Empty File

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读