Creating/writing into a new file in Qt

前端 未结 6 1615
陌清茗
陌清茗 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 <QFile>
    #include <QCoreApplication>
    #include <QTextStream>
    
    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;
    }
    
    0 讨论(0)
  • 2021-01-31 14:46

    Are you sure you're in the right directory?
    Opening a file without a full path will open it in the current working directory. In most cases this is not what you want. Try changing the first line to

    QString filename="c:\\Data.txt" or
    QString filename="c:/Data.txt"

    and see if the file is created in c:\

    0 讨论(0)
  • 2021-01-31 14:54

    It can happen that the cause is not that you don't find the right directory. For example, you can read from the file (even without absolute path) but it seems you cannot write into it.

    In that case, it might be that you program exits before the writing can be finished.

    If your program uses an event loop (like with a GUI application, e.g. QMainWindow) it's not a problem. However, if your program exits immediately after writing to the file, you should flush the text stream, closing the file is not always enough (and it's unnecessary, as it is closed in the destructor).

    stream << "something" << endl;
    stream.flush();
    

    This guarantees that the changes are committed to the file before the program continues from this instruction.

    The problem seems to be that the QFile is destructed before the QTextStream. So, even if the stream is flushed in the QTextStream destructor, it's too late, as the file is already closed.

    0 讨论(0)
  • 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 <QFile>
    #include <QTextStream>
    
    
    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).

    0 讨论(0)
  • 2021-01-31 15:00
    QFile file("test.txt");
    /*
     *If file does not exist, it will be created
     */
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text | QIODevice::ReadWrite))
    {
        qDebug() << "FAILED TO CREATE FILE / FILE DOES NOT EXIST";
    }
    
    /*for Reading line by line from text file*/
    while (!file.atEnd()) {
        QByteArray line = file.readLine();
        qDebug() << "read output - " << line;
    }
    
    /*for writing line by line to text file */
    if (file.open(QIODevice::ReadWrite))
    {
        QTextStream stream(&file);
        stream << "1_XYZ"<<endl;
        stream << "2_XYZ"<<endl;
    }
    
    0 讨论(0)
  • 2021-01-31 15:02

    Your code is perfectly fine, you are just not looking at the right location to find your file. Since you haven't provided absolute path, your file will be created relative to the current working folder (more precisely in the current working folder in your case).

    Your current working folder is set by Qt Creator. Go to Projects >> Your selected build >> Press the 'Run' button (next to 'Build) and you will see what it is on this page which of course you can change as well.

    0 讨论(0)
提交回复
热议问题