为什么我的输出txt文件是空的,打开文本文件时没有收到错误

问题描述

我想弄清楚为什么我的 ofstream 不会在 txt 文件中打印任何内容。 txt 文件已成功创建,打开所述文件时我没有收到任何错误。但是当我打开它时,它是空的。 ifstream 文件也成功打开并且读取没有错误。我在下面包含了一些代码

打开和检查

 outfile.open(outName.c_str());
if(outfile.fail())
{
    cout << "Cannot open output file .Aborting "<<endl;
    exit(1);
}

处理ifstream文件输出到ofstream文件

 char ch;
int count=0;
infile.get(ch);
while(!infile.eof())
{
    ch=tolower(ch);
    if(ch >= 'a' && ch <= 'z')
    {
        count++;
        if(count<=8)
            outfile << ch;

    }
    else
    {
        if(ch == ';')
        {
            outfile << ch;
            count = 0;
        }
    }
    infile.get(ch);
}

解决方法

也许您应该关闭文件以将缓冲区字符刷新到文件中。