C ++:.eof在空文件上 [英] C++: .eof on an empty file

查看:178
本文介绍了C ++:.eof在空文件上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们看看这个程式:

ifstream filein("hey.txt");


if(filein.eof()){
    cout<<"END"<<endl;
}

这里hey.txt所以这里的if条件应该是真的但它isnt

Here "hey.txt" is empty. So the if condition here is thought should have been true But it isnt

为什么eof返回true虽然文件是空的?

Why isnt the eof returning true although the file is empty?

如果我在之前添加了这个,如果 eof返回true,但 arr 文件仍为空,因此两者不变。

If i added this before the if the eof returns true although arr is still empty and the file is still empty so both unchanged

char arr[100];
filein.getline(arr,99);


推荐答案


eof()函数在程序尝试读取文件结束后返回true。

eof() function returns "true" after the program attempts to read past the end of the file.

您可以使用 std :: ifstream :: peek()来检查逻辑文件结束。

You can use std::ifstream::peek() to check for the "logical end-of-file".

这篇关于C ++:.eof在空文件上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆