简体   繁体   English

尽管已打开,ofstream 仍不打印

[英]ofstream not printing despite being open

I am trying to print data to a file;我正在尝试将数据打印到文件中; however, the file that is created is empty, and nothing seems to be printed to it.但是,创建的文件是空的,并且似乎没有打印任何内容。 I know that the file stream is open and good because my cout statements are printing what i am expecting, but for some reason, the write-file is blank when I check.我知道文件 stream 是打开的并且很好,因为我的 cout 语句正在打印我所期望的,但是由于某种原因,当我检查时写入文件是空白的。 Any solutions would be appreciated.任何解决方案将不胜感激。 Thanks.谢谢。

void printToStream(std::ofstream &fileStream, std::string printString) {
    if (fileStream.is_open() && fileStream.good()) {
        std::cout << "printing to file " << std::endl;
        std::cout << printString;
        fileStream << std::flush;
        fileStream << printString;
    } else {
        printError("File not open");
    }
}

Move std::flush to the end of the output.std::flush移动到 output 的末尾。

For example like this:例如像这样:

    fileStream << printString << std::flush;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM