Question

I try to write to an existing file with QFile, which works as expected. However, the problem is that if the file is open in Excel, writing to the file from my program fails.

I try to test the permissions with QFileInfo and have all read and write permissions on the file. The test

bool opened = file-> open (QIODevice :: WriteOnly) 

returns true.

The same problem does not occur when the file is opened with notepad++.

How can I check if the file is locked and can't be written to?

Was it helpful?

Solution

Excel locks its open files for exclusive use. You can't write to an open file, move or delete it. There is no way to bypass this lock.

See also: Write to locked file regardless of lock status

OTHER TIPS

When you use the QFile::write function, it returns the number of bytes written, or -1 if an error occurred.

If you check the return code from the write function, you should be able to use that to determine that the file is locked by another process.

Calling QFile::open returns without error, because you can still get a valid handle to the file, even though another process has locked it, preventing you writing to it at the same time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top