c++ fstream:当到达eof时抛出异常
C++ fstream: throwing exception when reaching eof
我想从两个文件中读取,直到到达其中一个文件的末尾。如果出错,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.read
或istream::get
while (input1.get(in1) && input2.get(in2)) {
...
}
如果读取循环体中的字符,则在输出中将有一个额外的字符,而没有相应的输入字符。也许这就是为什么你一开始就使用std::ios::exeptions
的原因。
只需删除.eof()
if(fstream)
检查所有位(eof bad和fail)
因此将while重写为:
while(input1 && input2)
然后可能验证eof()对至少一个流返回true。
相关文章:
- 从构造函数抛出异常时如何克服内存泄漏
- 函数如何通知用户它基于函数原型抛出异常?
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- VisualStudios 会抛出异常,而代码块不会 [C++]
- 如果不包含 pthread,为什么 GCC 的线程标准库实现会抛出异常?
- ZMQ::send() 抛出异常并终止 QNX 进程.为什么以及如何从中恢复?
- new(std::nothrow) int[n] 抛出异常
- 为什么g_object_set抛出异常(vcruntime140.dll)?
- 你能防止 std::regex 在无效表达式上抛出异常吗?
- C++子线程抛出异常
- 我们应该在抛出异常之前取消分配内存吗
- 有没有一种方法可以让OpenCLC++绑定为所有错误抛出异常
- 为什么访问模板化变体作为返回值抛出异常
- 如何实现,错误分配中止而不是抛出异常
- std::p riority_queue::p op什么时候会抛出异常
- 在0x5914F3BE抛出异常(基于.dll)
- std::vector 默认构造函数可以抛出异常吗?
- 挂钩创建文件抛出异常:读取访问冲突
- getaddrinfo 抛出异常
- c++ fstream:当到达eof时抛出异常