c++逐行读取txt文件getline漏掉第一行

#include
#include
#include
using namespace std;
int main()
{
ifstream fin;
fin.open("word.txt");
string str;
while (!fin.eof())

{
getline(fin, str);
fin >> str;
cout << str<< endl;
}
fin.close();
}

word.txt第一行始终显示不出来。求解

getline()已经把行从文件里读到str里了,你下一行加的 fin >> str是错误的。
去掉 fin >> str; 这一行就好了。

如果你行数很多,并且在控制台输出,那么可能前面的内容会超过缓冲区被丢弃。
你换一个短一点的文件

去掉 fin >> str;

getline方法定义没有看吧,已经读取进去了。