Creating/writing into a new file in Qt

前端 未结 6 1623
陌清茗
陌清茗 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:55

    That is weird, everything looks fine, are you sure it does not work for you? Because this main surely works for me, so I would look somewhere else for the source of your problem.

    #include 
    #include 
    
    
    int main()
    {
        QString filename = "Data.txt";
        QFile file(filename);
        if (file.open(QIODevice::ReadWrite)) {
            QTextStream stream(&file);
            stream << "something" << endl;
        }
    }
    

    The code you provided is also almost the same as the one provided in detailed description of QTextStream so I am pretty sure, that the problem is elsewhere :)

    Also note, that the file is not called Data but Data.txt and should be created/located in the directory from which the program was run (not necessarily the one where the executable is located).

提交回复
热议问题