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. Problem writing out to a stream

Problem writing out to a stream

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 516 Views
  • 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.
  • L Offline
    L Offline
    leinad
    wrote on last edited by
    #1

    Hi,

    I'm trying to write out to a file using QTextStream and although it compiles and runs, nothing goes to the file. Can't seem to figure what I'm doing wrong. Here is a simplified version. The file does get created in the directory just nothing writes to it.

    static QTextStream outStream; //keep the handle since we can go into the method multiple times
    static bool once = false;

    if(once == false)
    {
    once = true;
    QDir someDir(logDir); //logDir = some director c:"\blah\blah
    if(someDir.exists())
    {
    QString fileName = logDir + "/" + "myfileName";
    QFile file(filename);
    if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
    {
    outStream.setDevice(&file);
    }

    }
    }
    Qstring someText = "blah blah";
    outStream << someText << "\n";

    Any help would be appreciated.

    KroMignonK 2 Replies Last reply
    0
    • L Offline
      L Offline
      leinad
      wrote on last edited by
      #2

      I tried closing the file and sure enough the data was logged to the file, so I tried using file.flush() and nothing was written to the file. Any ideas how to force the data to the file?

      B 1 Reply Last reply
      0
      • L leinad

        I tried closing the file and sure enough the data was logged to the file, so I tried using file.flush() and nothing was written to the file. Any ideas how to force the data to the file?

        B Offline
        B Offline
        Bonnie
        wrote on last edited by
        #3

        @leinad What about outStream.flush()?

        1 Reply Last reply
        0
        • L Offline
          L Offline
          leinad
          wrote on last edited by
          #4

          Write, thanks. I saw in another thread to use both outsream.flush and Qfile.flush().

          B 1 Reply Last reply
          0
          • L leinad

            Hi,

            I'm trying to write out to a file using QTextStream and although it compiles and runs, nothing goes to the file. Can't seem to figure what I'm doing wrong. Here is a simplified version. The file does get created in the directory just nothing writes to it.

            static QTextStream outStream; //keep the handle since we can go into the method multiple times
            static bool once = false;

            if(once == false)
            {
            once = true;
            QDir someDir(logDir); //logDir = some director c:"\blah\blah
            if(someDir.exists())
            {
            QString fileName = logDir + "/" + "myfileName";
            QFile file(filename);
            if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
            {
            outStream.setDevice(&file);
            }

            }
            }
            Qstring someText = "blah blah";
            outStream << someText << "\n";

            Any help would be appreciated.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #5

            @leinad said in Problem writing out to a stream:

            static QTextStream outStream; //keep the handle since we can go into the method multiple times

            Why use static and not a class member?

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            0
            • L leinad

              Write, thanks. I saw in another thread to use both outsream.flush and Qfile.flush().

              B Offline
              B Offline
              Bonnie
              wrote on last edited by
              #6

              @leinad I think outStream.flush() should also have called file->flush() inside it though.

              1 Reply Last reply
              0
              • L leinad

                Hi,

                I'm trying to write out to a file using QTextStream and although it compiles and runs, nothing goes to the file. Can't seem to figure what I'm doing wrong. Here is a simplified version. The file does get created in the directory just nothing writes to it.

                static QTextStream outStream; //keep the handle since we can go into the method multiple times
                static bool once = false;

                if(once == false)
                {
                once = true;
                QDir someDir(logDir); //logDir = some director c:"\blah\blah
                if(someDir.exists())
                {
                QString fileName = logDir + "/" + "myfileName";
                QFile file(filename);
                if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
                {
                outStream.setDevice(&file);
                }

                }
                }
                Qstring someText = "blah blah";
                outStream << someText << "\n";

                Any help would be appreciated.

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by KroMignon
                #7

                @leinad said in Problem writing out to a stream:

                This code cannot work!!

                static QTextStream outStream;  //keep the handle since we can go into the method multiple times
                

                Why static??? please use class member for this!

                static bool once = false;
                

                What this and not use outStream.device() != nullptr to check if stream has been initialized?

                if(once == false)
                {
                    once = true;
                    QDir someDir(logDir);  //logDir = some director c:"\blah\blah
                    if(someDir.exists())
                    {
                       QString fileName = logDir + "/" + "myfileName";
                       QFile file(filename);
                       if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
                       {
                           outStream.setDevice(&file);
                       }
                    }
                

                ==> After here file is destroyed and closed and outStream use an invalid QIODevice!

                }
                Qstring someText = "blah blah";
                outStream << someText << "\n";
                

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                1

                • Login

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