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.
QtWS25 Last Chance

Closing a QFile in use.

Scheduled Pinned Locked Moved General and Desktop
qfileqiodevice
4 Posts 3 Posters 3.2k 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.
  • NIXINN Offline
    NIXINN Offline
    NIXIN
    wrote on 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.sueM Offline
      m.sueM Offline
      m.sue
      wrote on 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
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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.sueM 1 Reply Last reply
        3
        • mrjjM mrjj

          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.sueM Offline
          m.sueM Offline
          m.sue
          wrote on 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

          • Login

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