eof问题c ++ [英] eof problem c++

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

问题描述

我在Windows xp上使用Dev C ++

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;
}

此代码出现以下错误

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

推荐答案

如果您将循环改为

  while(getline(infile,STRING))
  {
     cout<<STRING; 
  }

您可以避免读取最后一次值两次的可能性(参见这是SO post )。

you avoid the possibility of reading the last value twice (see this SO post).

这篇关于eof问题c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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