Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot write simple text to a text file
Forum Updated to NodeBB v4.3 + New Features

Cannot write simple text to a text file

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 4.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sammy742
    wrote on 23 Jul 2013, 07:24 last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 23 Jul 2013, 07:32 last edited by
      #2

      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
      @

      (Z(:^

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on 23 Jul 2013, 07:39 last edited by
        #3
        1. open the file before you can read/write it.

        @
        QFile::open()
        @

        1. Be careful when using relative paths, when not familiar with CWD.
        1 Reply Last reply
        0
        • S Offline
          S Offline
          sammy742
          wrote on 23 Jul 2013, 08:15 last edited by
          #4

          //

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sammy742
            wrote on 23 Jul 2013, 09:08 last edited by
            #5

            @
            #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");

            1 Reply Last reply
            0
            • K Offline
              K Offline
              KA51O
              wrote on 23 Jul 2013, 09:11 last edited by
              #6

              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;
              }
              @

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sierdzio
                Moderators
                wrote on 23 Jul 2013, 09:14 last edited by
                #7

                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.

                (Z(:^

                1 Reply Last reply
                0

                1/7

                23 Jul 2013, 07:24

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved