用C++中的两个文件流处理异常
Handling exception(s) with two file stream in C++
我正在使用Visual Studio 2012构建一个处理输入文件和输出文件的程序。
我是这样实现的:
ifstream inputFile;
ofstream outputFile;
inputFile.exceptions ( ifstream::failbit | ifstream::badbit );
try
{
// some codes here
inputFile.open(inputFileName.c_str());
cout << "Input file opened" << endl;
outputFile.open(outputFileName.c_str());
cout << "Output file opened" << endl;
}
catch (ifstream::failure e)
{
cerr << "Failed to open input file" << endl;
return -1;
}
catch (ofstream::failure e)
{
cerr << "Failed to open output file" << endl;
return -1;
}
出现编译错误:
error C2312: 'std::ios_base::failure' : is caught by 'std::ios_base::failure' at line 248
如何实现具有两个异常源的try-catch?
您的问题是ifstream::failure
和ofstream::failure
是相同的类型(从ios_base继承给它们),
由于是同一个异常,编译器会抱怨。
顺便说一句,您应该通过const引用捕获,以避免不必要的副本。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string inputFileName { "/notexists" };
string outputFileName { "/notexistsandprobablynotwritable" };
ifstream inputFile { inputFileName };
if( !inputFile ) {
cerr << "Failed to open input file" << endl;
return -1;
}
cout << "Input file opened" << endl;
ofstream outputFile { outputFileName };
if( !outputFile ) {
cerr << "Failed to open output file" << endl;
return -1;
}
cout << "Output file opened" << endl;
}
或者,如果你真的需要例外,你可以自己在开放网站上抛出不同的例外:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
template<typename Stream, typename Exception = typename Stream::failure>
void try_open(Stream& s, string filename) {
s.open(filename);
if( !s )
throw Exception( string("cannot open ")+filename );
}
struct input_exception: ifstream::failure { input_exception(const string& s): ifstream::failure(s) {} };
struct output_exception: ofstream::failure { output_exception(const string& s): ofstream::failure(s) {} };
int main() {
string inputFileName { "/notexists" };
string outputFileName { "/notexistsandprobablynotwritable" };
try {
ifstream inputFile;
try_open<ifstream, input_exception>(inputFile, inputFileName);
cout << "Input file opened" << endl;
ofstream outputFile;
try_open<ofstream, output_exception>(outputFile, outputFileName);
cout << "Output file opened" << endl;
} catch(const output_exception& e) {
cerr << "output exception!n" << e.what() << "n";
} catch(const input_exception& e) {
cerr << "input exception!n" << e.what() << "n";
} catch(const exception& e) {
cerr << "exception!n" << e.what() << "n";
}
}
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 两个文件使用彼此的功能-如何解决
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 结构包含在两个头文件中,这两个文件我都不拥有
- Levenshtein 两个文件的距离花费了太多时间
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 在 txt 文件中显示前两个数字的程序
- 在 c++ 中从文本文件中读取两个十六进制值
- 将两个 cpp 文件合并为一个 cpp 文件
- JNI,使用两个 .so 文件时出错,其中一个文件需要另一个文件
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- 比较两个没有一行共同点的大文件
- 在两个.cpp文件之间定义全局类/结构指针
- C++ 如何将两个 makefile 对象目标规则(位于另一个文件夹中)合并到一个目标/规则中?
- 如何在c ++中以一行(水平)打印两个文件的数据?如给定的.这两种形式来自两个不同的文本文件
- 如何在 c++ 中比较两个文本文件
- 包括两个文件 C++ 之间的冲突
- 使用 C++ 读取具有两个连续分隔符的 csv 文件
- 如何检测两个文件条目是否引用与Qt相同的物理文件系统?