eof problem c++
eof problem 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 !
我不知道这里有什么问题,我不能编译代码请帮助
如果你将循环设置为
while(getline(infile,STRING))
{
cout<<STRING;
}
可以避免读取最后一个值两次的可能性(参见这篇文章)。
std::ifstream::eof
是一个返回bool值的函数。所以你必须把它命名为
infile.eof()
您在eof
后面忘记了()
相关文章:
- 无法让 Assimp 在 Windows " linker problem" 上运行?(与2019年相比)
- C++ Winsock unicode problem
- QT QStackedLayout problem
- Problem with Android ndk
- OpenGL SuperBible header problem
- WaitForSingleObject problem
- Anderson tree problem
- for_each C++ Problem
- Getline problem
- CDialog ShowWindow problem
- SLOT problem / C++
- BitBlt Problem GDI
- Problem with C# COM
- eof problem c++
- Problem with Euler 27