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. Closing a QFile in use.
Forum Updated to NodeBB v4.3 + New Features

Closing a QFile in use.

Scheduled Pinned Locked Moved General and Desktop
qfileqiodevice
4 Posts 3 Posters 3.2k Views 2 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.
  • N Offline
    N Offline
    NIXIN
    wrote on 30 Nov 2016, 12:34 last edited by
    #1

    I am writing to a .csv file as below:

    QString fileName = "someFile.csv";

    QFile file(fileName);

    if(file.exists())
    file.remove();

    if(file.open(QIODevice::WriteOnly | QIODevice::Truncate)
    {
    //write something to file as comma separated string

    file .close();
    }

    Above code is working fine.

    But when file is already opened (double click and open the file from the path where it is created), and I run my application, data is not being written to the file. Earlier data exists.

    How can I fix this?

    ALso is it possible to close the file if it is already opened (double click and open the file from the path where it is created), programmatically.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      m.sue
      wrote on 30 Nov 2016, 13:14 last edited by
      #2

      Hi,
      you are working on WINDOWS I assume.

      On WINDOWS it is possible to open a file for writing that another app already has open for reading if the other app opened the file with the OF_SHARE_DENY_NONE option or similar, which is not the default. So all in all it depends on the other app where you may not have the controll over (i.e. source from).

      You can close files (handles) opened by other apps only with elevated rights.
      -Michael.

      1 Reply Last reply
      3
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 30 Nov 2016, 14:32 last edited by
        #3

        Hi
        The normal case for this is to check the return code of open and if it fails
        output a message to user that file is locked.
        http://doc.qt.io/qt-5/qfiledevice.html#error

        M 1 Reply Last reply 30 Nov 2016, 14:38
        3
        • M mrjj
          30 Nov 2016, 14:32

          Hi
          The normal case for this is to check the return code of open and if it fails
          output a message to user that file is locked.
          http://doc.qt.io/qt-5/qfiledevice.html#error

          M Offline
          M Offline
          m.sue
          wrote on 30 Nov 2016, 14:38 last edited by
          #4

          @mrjj said in Closing a QFile in use.:

          output a message to user that file is locked

          Yes, as the other app would most certainly crash if it suddenly works on a file handle that was closed from outside.
          -Michael.

          1 Reply Last reply
          1

          2/4

          30 Nov 2016, 13:14

          • Login

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