【问题标题】:Last file line reading gives different results最后文件行读取给出不同的结果
【发布时间】:2015-09-24 03:59:46
【问题描述】:

我正在尝试从具有以下内容的文件中读取字符串:

r FFFF

r FF

r FFFF

这是我的代码:

int main ()
{   int a; ifstream ifile; 
   ifile.open("hi.txt"); 
  while (!ifile.eof()) 
   { 
      ifile.getline(data, 100); // read a line from file
      a = strlen(data);
      cout<<'length"<<a;
    }
}

我得到了意想不到的输出:length7length5length6

我做错了什么?

【问题讨论】:

标签: c++ file-handling strlen file-read


【解决方案1】:

'\n'(换行符)在data 中。在 Windows 上,您可能还有一个 '\n\r' 代替(或 '\r\n' 我不记得了)

在最后一行,您的文本文件中可能没有它

【讨论】:

    猜你喜欢
    • 2022-10-15
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    相关资源
    最近更新 更多