读取文件会引发异常
Reading a file throws an exception
有人知道我做错了什么吗
inputFileName = argv[1];
outputFileName = argv[2];
std::ifstream readFile;
readFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
//set the flags for stream bits that indicate failure if ON
std::ofstream writeFile;
writeFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
try{
readFile.open(inputFileName);
writeFile.open(outputFileName);
//do some stuff
readFile.close();
writeFile.close();
}
catch(std::ifstream::failure &readErr) {
std::cerr << "nnException occured when reading a filen"
<< readErr.what()
<< std::endl;
return -1;
}
catch(std::ofstream::failure &writeErr) {
std::cerr << "nnException occured when writing to a filen"
<< writeErr.what()
<< std::endl;
return -1;
}
编译时我得到
warning: exception of type 'std::ios_base::failure' will be caught [enabled by default]
catch(std::ofstream::failure &writeErr) {
^
warning: by earlier handler for 'std::ios_base::failure' [enabled by default]
catch(std::ifstream::failure &readErr) {
^
当我运行代码时,会打印Exception occured when reading a file
和readErr.what()basic_ios::clear
。
我查了很多例子,看不出哪里出了问题。此外,如果有帮助的话,我在Ubuntu 14.04上。
是否存在inputFileName?只需写:
catch ( std::exception const& e ) {
std::cerr << "Exception: " << e.what() << std::endl;
}
我打印了argv[0]
,发现IDE实际上在不同的目录中运行程序。抱歉我的疏忽
相关文章:
- 加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常
- 读取文件时引发异常
- 尝试将数字写入二进制文件时引发异常
- 为什么我尝试在 OpenGL 中加载的 3ds 文件会出现此未经处理的异常?
- 如何构建一个异常类来报告C++中的哪些文件和行号?
- 从二进制文件中读取元素时引发异常(引发异常:读取访问冲突. _Pnext 0xB414D4)
- 读取 MAC 地址时文件读取异常
- free():在有效指针异常中-使用流读取巨大文件时
- 尝试将 std::cerr 重定向到文件时出现访问冲突异常
- 文件流析构函数是否可以在C++中引发异常?
- 挂钩创建文件抛出异常:读取访问冲突
- 仅捕获异常就可以检测所有二进制文件在C 中读取错误是否足够
- 如果文件为c ,如何抛出异常
- 尝试使用 wxWidgets 从 zip 打开不存在的文件时无法捕获的异常
- 加载C DLL文件时,如何修复异常_access_violation
- C++ 读取文件和写入变量时未处理的异常 - getline 错误?
- G++ 和 CL(VS2017) 都编译成功,但 cl 编译的可执行文件运行异常
- 食人魔异常(6:文件未找到异常):找不到'ogre.cfg'文件
- 系统访问出界异常,尝试从flatbuffer的二进制文件访问"LengthofTable"
- 提升序列化在从二进制文件读取时引发异常