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. QFile: Append mode does not work well.
QtWS25 Last Chance

QFile: Append mode does not work well.

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 12.9k 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
    Landy
    wrote on last edited by
    #1

    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
    0
    • R Offline
      R Offline
      rcari
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #3

        [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
        0
        • L Offline
          L Offline
          Landy
          wrote on last edited by
          #4

          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
          0
          • L Offline
            L Offline
            Landy
            wrote on last edited by
            #5

            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
            0
            • D Offline
              D Offline
              dbzhang800
              wrote on last edited by
              #6

              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
              0
              • L Offline
                L Offline
                Landy
                wrote on last edited by
                #7

                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
                0

                • Login

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