我想从两个文件中读取,直到读到其中一个文件的末尾。如果出现错误,fstream应该抛出一个异常。
问题是,当eof位被设置时,坏或失败位也被设置。
ifstream input1;
input1.exceptions(ios_base::failbit | ios_base::badbit);
input1.open("input1", ios_base::binary | ios_base::in);
ifstream input2;
input2.exceptions(ios_base::failbit | ios_base::badbit);
input2.open("input2", ios_base::binary | ios_base::in);
ofstream output;
output.exceptions(ios_base::failbit | ios_base:: badbit);
output.open("output", ios_base::binary | ios_base::out | ios_base::trunc);
char in1, in2, out;
while(!input1.eof() && !input2.eof()) {
input1.read((char*) &in1, 1);
input2.read((char*) &in2, 1);
out = in1^in2;
output.write((const char*) &out, 1);
}
input1.close();
input2.close();
output.close();这导致了
$ ./test
terminate called after throwing an instance of 'std::ios_base::failure'
what(): basic_ios::clear怎样做才是正确的?
发布于 2013-01-14 04:07:19
代码中的基本问题是FAQ。您永远不应该使用eof()作为读取循环的测试条件,因为在C/C++ (与其他一些语言不同)中,直到读取超过文件结尾时,eof()才会设置为true,因此循环体将被多次输入一次。
惯用的正确过程是让读取操作本身处于循环条件中,以便退出发生在正确的点上:
while ( input1.get(in1) && input2.get(in2) ) { /* etc */ }
// here, after the loop, you can test eof(), fail(), etc
// if you're really interested in why the loop ended.此循环将在耗尽较小的输入文件后自然结束,这正是您想要的。
发布于 2013-01-14 00:54:59
只需移除.eof() if(fstream)检查所有位(eof bad和fail)。
因此,将while重写为:
while(input1 && input2)然后可以验证eof()对于最后一个流是否返回true。
希望这能有所帮助。
发布于 2013-01-14 01:07:59
根本不要抛出异常,在while条件中使用input1.read或istream::get
while (input1.get(in1) && input2.get(in2)) {
...
}如果您读取循环体中的字符,则在输出中将有一个额外的字符,没有相应的输入字符。也许这就是为什么你一开始使用std::ios::exeptions的原因。
https://stackoverflow.com/questions/14305722
复制相似问题