此代码不包含ofstream对象file1中文件计数的字符串。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
ofstream file1("count.txt");
string sentence;
getline(cin,sentence);
file1<<sentence;
ifstream file2("count.txt");
string wordscount;
while(getline(file2,wordscount))
{
cout<<wordscount;
}
}
您尚未关闭文件,因此FS上可能不存在该文件。添加行
file1.close(); // <-- new line
ifstream file2("count.txt");