我正在尝试读取具有以下内容的文件:
This is Line One
This is Line Two
This is Line Three
This is Line Four
This is Line Five
代码:
#include <iostream>
#include <fstream>
#include <limits>
using namespace std;
int main()
{
char buff[50];
ifstream is("Test.txt");
if(!is)
cout << "Unable to open Test.txt\n";
while(is.getline(buff,50,'\n'))
{
cout << is.gcount();
cout << buff;
cout << "\n----------------\n";
}
return 0;
}
输出:
$ ./Trial
18This is Line One
----------------
18This is Line Two
----------------
20This is Line Three
----------------
19This is Line Four
----------------
17This is Line Five
----------------
现在假设如果我评论cout << "\n----------------\n";
即
while(is.getline(buff,50,'\n'))
{
cout << is.gcount();
cout << buff;
//cout << "\n----------------\n";
}
我得到输出为:
$ ./Trial
17This is Line Fivee
我无法弄清楚 - 为什么会出现这种行为?
另外为什么将计数显示为 18(假设第一行 - 其中第一行包含 16 个字符,包括空格 - 如果我添加 null 它变为 17 - 行尾字符被 getline 丢弃)。
我正在使用 windows-7 和 cygwin。