我试图编写代码以计算文件行中的字符数,但是ifstream对象不占用文件中的行数

问题描述 投票:0回答:1

此代码不包含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;
    }
}
c++ file-handling
1个回答
1
投票

您尚未关闭文件,因此FS上可能不存在该文件。添加行

file1.close();  // <-- new line
ifstream file2("count.txt");
© www.soinside.com 2019 - 2024. All rights reserved.