<分区>
我设法写入了文本文件,但我读取的文件出了点问题。这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line, s;
ofstream out_file;
out_file.open("hello.txt");
for(int count = 0; count< 5; count++)
{
out_file << "Hello, World" << endl;
}
out_file.close();
ifstream in_file;
in_file.open("hello.txt");
if (in_file.fail())
{
cout << "File opening error. " << endl;
}
else
{
while(getline(in_file,line))
{
in_file >> s;
cout << s << endl;
}
}
in_file.close();
system("Pause");
return 0;
}
我设法将 5 次“Hello, World”写入一个文本文件。然而,当程序运行时,它只打印了 4 次“Hello”,并在第 5 行打印了“World”。从我的代码来看,它不是应该打印 5 次“Hello, World”吗?有人可以指出错误在哪里吗?