c++ fstream:当到达eof时抛出异常

C++ fstream: throwing exception when reaching eof

本文关键字:eof 抛出异常 fstream c++      更新时间:2023-10-16

我想从两个文件中读取,直到到达其中一个文件的末尾。如果出错,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

如何做正确?

代码中的基本问题是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.

这个循环将在较小的输入文件耗尽时自然结束,这正是您想要的。

不要抛出异常,在while条件中使用input1.readistream::get

while (input1.get(in1) && input2.get(in2)) {
...
}

如果读取循环体中的字符,则在输出中将有一个额外的字符,而没有相应的输入字符。也许这就是为什么你一开始就使用std::ios::exeptions的原因。

只需删除.eof() if(fstream)检查所有位(eof bad和fail)

因此将while重写为:

 while(input1 && input2)

然后可能验证eof()对至少一个流返回true。