Closing a QFile in use.
-
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 stringfile .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.
-
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. -
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