在Qt、c++中获取最后的错误编号
Get the last error number in Qt, C++
我正在测试一个应该创建并写入文件的函数,该文件的名称作为函数的参数给出。函数失败时返回false。是否有任何方法可以在windows上获得像GetLastError()这样的错误编号??
对于QFile类的方法,您可以使用返回FileError enum的方法error
如果您对获取文件操作的"最后"错误感兴趣,您可以执行以下操作:
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
enum而不是字符串。
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- Project Euler问题4的错误解决方案
- 不断收到相同的错误...怎么了?C++
- 0-1背包代码中的错误.我的代码中有什么错误
- 在c++中实现LinkedList时,应出现未处理的错误
- 有人知道为什么在开关中使用stoi函数会返回恒定的错误吗
- 尝试将lambda函数放在队列中时出现一般分配器错误(可能是与unique_ptr有关的错误)
- 是什么导致了Unity 3D中的"错误线程异常"?
- strncpy之后的char数组的错误行为
- posix_spawn():使用posix_scawn()时的错误处理问题
- 这些是什么样的错误?即使我不在 Linux 上工作,我也遇到了 Linux 错误
- 如何防止初始值设定项列表中的错误值?
- C++包含来自 #include "DevEngine/Core.h" 的错误
- 我在范围内未声明的错误类有问题
- 声明为无效的变量或字段'...' Ardunio 编译器上的错误
- 尝试了解在导入的静态方法上使用删除方法时的错误
- 如何在C++中使用 Void 而不会收到有关参数的错误
- 在队列上的最后一个操作循环中的错误
- 获取最后一个错误的宽字符版本
- 在Qt、c++中获取最后的错误编号