Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-26410

Multiple file opening and QFile rename

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 4.7.2
    • Core: I/O
    • None
    • Qt Creator

    Description

      Please, look at this code:

      #include <QFile>
      #include <QDebug>
      #include <QDir>
      
      int main()
      {
          QString qs_Name_1("_001.txt");
          QString qs_Name_2("_002.txt");
      
          QFile::remove(qs_Name_1);
          QFile::remove(qs_Name_2);
      
          QFile file_1(qs_Name_1);
          QFile file_2(qs_Name_1);
      
          file_1.open(QIODevice::ReadWrite);
          file_2.open(QIODevice::ReadWrite);
      
          qDebug() << "rename:" << qs_Name_1 << "-->" << qs_Name_2 << "\t result:" << file_1.rename(qs_Name_2);
          qDebug() << "file_1 open mode:" << file_1.isOpen();
          qDebug() << "file_2 open mode:" << file_2.isOpen();
          qDebug() << "file_2 name:" << file_2.fileName();
      
          file_2.write(QString("WTF?").toUtf8());
      
          qDebug() << "file_2 size:" << file_2.size() << "bytes";
      
          qDebug() << QFile(qs_Name_1).open(QIODevice::ReadOnly);
      
          qDebug() << QDir().entryList();
      
          return 0;
      }

      ...and it's out:

      rename: "_001.txt" --> "_002.txt" 	 result: true 
      file_1 open mode: false 
      file_2 open mode: true 
      file_2 name: "_001.txt" 
      file_2 size: 4 bytes 
      false 
      (".", "..", "_002.txt", "main.cpp", "main.o", "Makefile", "badfile", "badfile.pro", "badfile.pro.user")
      

      I create two instances of QFile with two identical fileNames and open both in Read|Write mode. After that, the file with name qs_Name_1 appears in the program directory. If I try to rename the first instance of QFile, it'll be closed and renamed, but the second one saves the connection to the file with name qs_Name_2, while its fileName() method returns qs_Name_1.

      Attachments

        1. main.cpp
          0.8 kB
          Petro
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Eddy Edward Welbourne
            kukuyok Petro
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes