Creating/writing into a new file in Qt

前端 未结 6 1622
陌清茗
陌清茗 2021-01-31 14:31

I am trying to write into a file and if the file doesn\'t exist create it. I have searched on the internet and nothing worked for me.

My code looks currently like this:<

6条回答
  •  一个人的身影
    2021-01-31 14:44

    #include 
    #include 
    #include 
    
    int main(int argc, char *argv[])
    {
        // Create a new file     
        QFile file("out.txt");
        file.open(QIODevice::WriteOnly | QIODevice::Text);
        QTextStream out(&file);
        out << "This file is generated by Qt\n";
    
        // optional, as QFile destructor will already do it:
        file.close(); 
    
        //this would normally start the event loop, but is not needed for this
        //minimal example:
        //return app.exec();
    
        return 0;
    }
    

提交回复
热议问题