c++qtqfileqtextstream

Qt (C++): QFile creates text file successfully but does not write to it


I have the following code, with which I am attempting to write to a file. When it is called, the file is created in the directory and the for-loop is entered. The values for in QVector<int> program also exist and are visible with qDebug(). However, after I close the file and the window, I check the file on my computer and it is completely empty. I have checked all over StackOverflow and the Qt forums and have yet to find a solution.

    QString save_file = "C:/Users/MARVIN/Documents/Saddleback College/2015/Fall/CS3A/Semester Project/Emulator/hello.txt";

    QFile file(save_file);

    if(file.open(QFile::WriteOnly))
    {
        QTextStream out(&save_file);

        out << "hello" << endl;

        for(int i = 0; i < 100; i++)
        {
            out << program[i] << endl;
            qDebug() << program[i] << endl;
        }

        file.close();
        this->close();
    }

Solution

  • Your issue:

    QTextStream out(&save_file);
    

    should be

    QTextStream out(&file);