我正在测试一个应该创建并写入文件的函数,该文件的名称作为函数的参数给出。该函数在失败时返回 false。有没有办法在 Windows 上获取像 GetLastError() 这样的错误号?
最佳答案
如果您有兴趣获取文件操作的“最后”错误,您可以执行以下操作:
bool makeSomethingWithFile(const QString &fileName, QString *error)
{
QFile file(fileName);
// Perform something with the file
// ...
// On error
if (file.error() != QFile::NoError) {
*error = file.errorString();
return false;
}
return true;
}
调用此函数时,只需提供错误字符串:
QString error;
if (!makeSomethingWithFile("myfile", &error)) {
qDebug() << "The error occurred:" << error;
}
好吧,如果您需要错误作为数字,请使用 FileError
枚举而不是字符串。
关于c++ - 获取Qt、C++中的最后一个错误号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24302610/