Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QFile: Append mode does not work well.

    General and Desktop
    3
    7
    11907
    Loading More Posts
    • 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
      Landy last edited by

      Hi guys,

      I met a problem when i use QFile in Append mode.
      here is the code:
      @#include <QtCore/QCoreApplication>
      #include <QFile>
      int main(int argc, char *argv[])
      {
      QCoreApplication a(argc, argv);

      QFile file&#40;"C:/test.txt"&#41;;
      bool ret;
      file.open(QIODevice::WriteOnly);
      file.write("this is string 1.");
      file.close();
      file.open(QIODevice::Append);
      file.write("this is string 2.");
      file.close();
      
      return a.exec&#40;&#41;;
      

      }@

      the problem is that, string 2 can not be writen in correct pos. acctually, when i open the file with QIODevice::Append, the pos of the file is always at the begining, even i use seek() to adjust the pos.

      and a good news is that, if i use a new instance of QFile to open same file with Append mode, it works well.
      i am really confused.

      1 Reply Last reply Reply Quote 0
      • R
        rcari last edited by

        Have you tried:
        @file.open( QIODevice::WriteOnly | QIODevice::Append );@
        on your second opening of the file ?

        1 Reply Last reply Reply Quote 0
        • D
          dbzhang800 last edited by

          [quote author="Landy" date="1349686182"]Hi guys,

          the problem is that, string 2 can not be writen in correct pos.
          [/quote]

          Hi, I can't reproduce this problem under Windows using Qt4.8 and Qt5.0.

          1 Reply Last reply Reply Quote 0
          • L
            Landy last edited by

            I tried. it didn't work.
            [quote author="rcari" date="1349686786"]Have you tried:
            @file.open( QIODevice::WriteOnly | QIODevice::Append );@
            on your second opening of the file ?[/quote]

            1 Reply Last reply Reply Quote 0
            • L
              Landy last edited by

              I am using static version of Qt4.7.4 and IDE is VS2010.
              it's really strange that u can't reproduce this problem.
              i hope this is not caused by Qt version.
              [quote author="1+1=2" date="1349745527"][quote author="Landy" date="1349686182"]Hi guys,

              the problem is that, string 2 can not be writen in correct pos.
              [/quote]

              Hi, I can't reproduce this problem under Windows using Qt4.8 and Qt5.0.
              [/quote]

              1 Reply Last reply Reply Quote 0
              • D
                dbzhang800 last edited by

                Oh, Seems that you have been misused "QIODevice::Append" flag.

                bq. QIODevice::Append 0x0004 The device is opened in append mode, so that all data is written to the end of the file.

                If you don't always want to append contents to the end of the file, you should not use this flag. In other words, if your need QIODevice::Append, you won't care position and seek() etc.

                Debao

                1 Reply Last reply Reply Quote 0
                • L
                  Landy last edited by

                  Acctually, as u see, i didn't use position or seek() in my code.
                  i really wanna append contents to the end of the file, but it didn't work to use QIODevice::Append flag.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post