QFile myFile.copy(newFile) failed, but myFile.error returns 0 (this means NoError)

StackOverflow https://stackoverflow.com/questions/15571560

  •  29-03-2022
  •  | 
  •  

質問

I am using Qt 4.6.3 with SUSE 11 Linux and I try to copy a file with QFile:

QFile myFile ("/my/path/myFile.txt");

if (!myFile.copy("/my/otherpath/myNewFile.txt")){
    cout << "Qt error: " << myFile.error() << endl;
}

If there is enough place on the device, everything is ok.

In case of disk is full and if I try to copy the file in a linux console with bash, then I got an error message.

cp /my/path/myFile.txt /my/path/myFile.txt
cp: writing `/my/path/myFile.txt': No space left on device

In my C++ program myFile.copy() returns "false", but myFile.error() returns '0'. I expected that myFile.error() returns a value not '0.

Furthermore I tried myFile.errorString() and have got "Unknown error" as the result.

Is it possible to get an errorcode or message like "No space left on device" ?

役に立ちましたか?

解決

Most probably the target file already exists. Documentation http://doc.qt.digia.com/4.6/qfile.html#copy says:

If a file with the name newName already exists, copy() returns false.

This is not an actual error condition, because it is normal for copy() not to overwrite. Check for its existence. And when you are inspecting for file error, do it with the error enum, eg:

QFile from("fromFile");
QFile target("targetFile");
if (target.exists())
    target.remove();
if (target.error() != QFile::NoError)
    // remove error
from.copy(target.fileName());
if (from.error() != QFile::NoError)
    // copy error
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top