代码之家  ›  专栏  ›  技术社区  ›  Mohamed Hamdy

写入文件时没有输出

c++
  •  0
  • Mohamed Hamdy  · 技术社区  · 6 年前

    文件输出为空,任何代码都会出现此问题 注意:方向文件为真。

    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
        ofstream myfile ("example.txt");
        while (myfile.is_open()) {
            myfile << "This is a line.\n";
            myfile << "This is another line.\n";
            myfile.close();
        }
        return 0;
    }
    
    0 回复  |  直到 6 年前
        1
  •  -1
  •   Muhammad bakr    6 年前

    首先确保文件已打开。

    你可以试试这个

    ofstream myfile ("example.txt" , ios::out);