eof 问题 c++
我在 windows xp 上使用 Dev C++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string STRING;
ifstream infile;
infile.open ("sample.txt");
while(!infile.eof)
{
getline(infile,STRING);
cout<<STRING;
}
infile.close();
return 0;
}
这段代码给出了以下错误
C:\C++\read.cpp: In function `int main()':
C:\C++\read.cpp:11: error: could not convert `infile.std::basic_ios<_CharT, _Traits>::eof [with _CharT = char, _Traits = std::char_traits<char>]' to `bool'
C:\C++\read.cpp:11: error: in argument to unary !
我不确定这里出了什么问题我无法编译代码 请帮忙
i am using Dev C++ on windows xp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string STRING;
ifstream infile;
infile.open ("sample.txt");
while(!infile.eof)
{
getline(infile,STRING);
cout<<STRING;
}
infile.close();
return 0;
}
this codes gives the following error
C:\C++\read.cpp: In function `int main()':
C:\C++\read.cpp:11: error: could not convert `infile.std::basic_ios<_CharT, _Traits>::eof [with _CharT = char, _Traits = std::char_traits<char>]' to `bool'
C:\C++\read.cpp:11: error: in argument to unary !
i am not sure what is wrong here i cant compile the code
please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您将循环更改为
避免读取最后一个值两次的可能性(请参阅这篇文章)。
If you change your loop to
you avoid the possibility of reading the last value twice (see this SO post).
std::ifstream::eof
是一个返回 bool 的函数。因此你必须这样称呼它std::ifstream::eof
is a function that returns a bool. Thsu you have to call it like您忘记了
eof
之后的()
。You forgot the
()
after theeof
.