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. [solved] QTextstream problems
Forum Updated to NodeBB v4.3 + New Features

[solved] QTextstream problems

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 11.3k 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
    spode
    wrote on last edited by
    #1

    in header, QFile *file_contenente_elencoParole; QTextStream *leggi_scrivi;

    @
    Gestore::Gestore(QObject *parent) :
    QObject(parent)
    {
    QDir dir;
    dir.mkpath("C:/WoerterErinnerung");
    file_contenente_elencoParole = new QFile("C:/WoerterErinnerung/elencoParoleSalvate.txt");

    if(file_contenente_elencoParole->open(QFile::Text | QFile::ReadWrite | QFile::Append))
    {
        leggi_scrivi = new QTextStream(file_contenente_elencoParole);
        leggi_scrivi->setCodec("UTF-8");
    }
    leggi_scrivi.operator <<("NEW LINE THAT LEGGI_SCRIVI CAN NOT WRITE....");
    

    }
    @

    why leggi_scrivi do not write into the file?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      You're

      leaking memory

      not flushing the QTextStream

      not closing the QFile

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • O Offline
        O Offline
        octal
        wrote on last edited by
        #3

        bq. in header, QFile *file_contenente_elencoParole; QTextStream *leggi_scrivi;

        How do you justify the usage of pointers here (except for leaking memory :D) ?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          Why do you use pointers here?
          I would prefer using local stack variables:

          @
          Gestore::Gestore(QObject *parent) :
          QObject(parent)
          {
          QDir dir;
          dir.mkpath("C:/WoerterErinnerung");
          QFile file_contenente_elencoParol("C:/WoerterErinnerung/elencoParoleSalvate.txt");

          if(file_contenente_elencoParole.open(QFile::Text | QFile::ReadWrite | QFile::Append))
          {
              QTextStream leggi_scrivi(&file_contenente_elencoParole);
              leggi_scrivi.setCodec("UTF-8");
              leggi_scrivi  << "NEW LINE THAT LEGGI_SCRIVI CAN NOT WRITE....";
          }
          

          }
          @

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            spode
            wrote on last edited by
            #5

            i would to use leggi_scrivi as a global variable...

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              Global? uähhh....
              perhaps as member in a class, but I would not use globaö variiables if possible.

              But you could call "QTextStream::flush":http://doc.qt.nokia.com/4.7/qtextstream.html#flush

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                If you are using [[Doc:QTextStream]] then please do not set the QFile::Text flag, that's useful for plain files only.

                Also, you should use QIODevice::xxx flags, as those are defined in [[Doc:QIODevice]] not in [[Doc:QFile]].

                This works:

                @
                QFile file("/tmp/qdn-test.txt");
                if(file.open(QIODevice::WriteOnly | QIODevice::Append)) {
                QTextStream ts(&file);
                ts.setCodec("UTF-8");
                ts << "Test for DevNet Users" << "\n";
                ts << "more text" << "\n";
                file.close();
                }
                @

                The file contents are likely to be not written to the disk until the file is closed, so you might see some leaks.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  spode
                  wrote on last edited by
                  #8

                  ok, also why this snippet dows not work?
                  @
                  QFile file_contenente_elencoParole("C:/WoerterErinnerung/elencoParoleSalvate.txt");
                  if(file_contenente_elencoParole.open(QIODevice::ReadWrite | QIODevice::Append))
                  {
                  QTextStream leggi_scrivi(&file_contenente_elencoParole);
                  leggi_scrivi.setCodec("UTF-8");
                  leggi_scrivi << "prova" << endl;
                  qDebug() << leggi_scrivi.readLine();
                  }
                  @

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dangelog
                    wrote on last edited by
                    #9

                    Can you define "does not work"? No file is created? The file is created but it's empty? The contents are different from the expected ones?

                    Software Engineer
                    KDAB (UK) Ltd., a KDAB Group company

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      spode
                      wrote on last edited by
                      #10

                      QTextStream writes but does not read...
                      qDebug() << leggi_scrivi.readLine();
                      returns "".

                      1 Reply Last reply
                      0
                      • O Offline
                        O Offline
                        octal
                        wrote on last edited by
                        #11

                        I guess you're at the end of the device. Thus you get an empty string when trying to read.

                        I would call @leggi_scrivi.seek(0)@ before

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          spode
                          wrote on last edited by
                          #12

                          thank you!!!! you are simply a wizard!

                          1 Reply Last reply
                          0

                          • Login

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