Closing a QFile in use.
I am writing to a .csv file as below:
QString fileName = "someFile.csv";
if(file.open(QIODevice::WriteOnly | QIODevice::Truncate)
//write something to file as comma separated string
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.
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_NONEoption 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.
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.
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.