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?

有帮助吗?

解决方案

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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top