Cannot write simple text to a text file



  • I am not able to write any text to my text files. When I go to 'File' > 'Open File or Project', I can see that 'txt1.txt' is in the same directory as main.cpp. These files are both in the 'startProgram' folder that contains the entire application. In 'startProgram.pro' it shows the following;

    QT += core
    QT -= gui
    TARGET = startProgram
    CONFIG += console
    CONFIG -= app_bundle
    TEMPLATE = app
    SOURCES += main.cpp
    OTHER_FILES +=
    txt1.txt \


    This works in eclipse, but not in QT:

    @
    #include <iostream>
    #include <fstream>
    using namespace std;

    int main ()
    {
    ofstream myfile;
    myfile.open ("example.txt");
    myfile << "Writing this to a file.\n";
    myfile.close();
    return 0;
    @

    This doesn't work in QT either:

    @
    #include <QtCore/QCoreApplication>
    #include <QFile>
    #include <QTextStream>

    int main ()
    {
    QString filename="txt1.txt";
    QFile file( filename );
    QTextStream stream( &file );
    stream << "something" << endl;

    return 0;
    

    }
    @

    Sincerely, sammy


  • Moderators

    You need to open the file before writing. See the documentation for QFile.
    @
    QFile file("txt1.txt");

    if (file.open(QIODevice::Write | QIODevice::Text)) {
    // now write using QTextStream or QByteArray
    file.write("my text");
    }

    file.close(); // not needed, file is closed in constructor
    @



    1. open the file before you can read/write it.

    @
    QFile::open()
    @

    1. Be careful when using relative paths, when not familiar with CWD.


  • //



  • @
    #include <iostream>
    #include <QCoreApplication>
    #include <QFile>
    #include <QTextStream>
    using namespace std;

    int main()
    {

    QFile file("C:/Qt/Qt5.0.2/Tools/QtCreator/bin/startProgram/txt1.txt";

    if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {

    cout << "it works!!!" << endl;

    // now write using QTextStream or QByteArray

    QTextStream stream( &file );
    file.write("my text\n");
    stream << "Written text in Qt\n";}

    return 0;
    }
    @

    When I used 'QFile file("txt1.txt");' the console printed 'it works!!!' , but the txt1.txt file was not showing the written text. Everything worked correctly after entering the full path of the text file :
    QFile file("C:/Qt/Qt5.0.2/Tools/QtCreator/bin/startProgram/txt1.txt");



  • you forgot the QFile::close call.

    Also please use "@" tags around your code to make it readable. This way you can easily spot all the mistakes you made in your code.
    @
    #include <iostream>
    #include <QCoreApplication>
    #include <QFile>
    #include <QTextStream>
    using namespace std;

    int main()
    {
    // MISSING ")" closing bracket
    QFile file(“C:/Qt/Qt5.0.2/Tools/QtCreator/bin/startProgram/txt1.txt”;
    if (file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
    cout << “it works!!!” << endl;
    // now write using QTextStream or QByteArray QTextStream stream( &file );
    file.write(“my text\n”);
    stream << “Written text in Qt\n”;
    }
    // MISSING QFILE::CLOSE() CALL
    return 0;
    }
    @


  • Moderators

    Please always wrap your code in '@' tags. Modifying your old posts will be a nice bonus, too :)

    As 1+1=2 said: take care about relative paths, they can be misleading at times.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.