C++ 当';标准:fstream';无法打开?
有没有办法让C++ 当';标准:fstream';无法打开?,c++,c++11,exception,filestream,c++-standard-library,C++,C++11,Exception,Filestream,C++ Standard Library,有没有办法让fstream在无法打开文件时抛出异常,我该怎么做 我知道std::fstream.exceptions()方法以及如何为其分配位,但是什么“位”会引发异常 iostream可以是三种不同的故障状态中的任意一种:fail、bad和eof。向您清楚地展示了如何使其中任何一个抛出异常;默认情况下,它们都不会,但如果出现任何故障模式,则在布尔上下文中计算流对象将返回false 示例: #include <fstream> std::ifstream infile; infil
fstream
在无法打开文件时抛出异常,我该怎么做
我知道std::fstream.exceptions()方法以及如何为其分配位,但是什么“位”会引发异常 iostream可以是三种不同的故障状态中的任意一种:fail、bad和eof。向您清楚地展示了如何使其中任何一个抛出异常;默认情况下,它们都不会,但如果出现任何故障模式,则在布尔上下文中计算流对象将返回
false
示例:
#include <fstream>
std::ifstream infile;
infile.expections(std::ios::failbit);
infile.open("foo.txt"); // throws on failure
#包括
std::ifstream-infle;
填充期望值(std::ios::failbit);
infle.open(“foo.txt”);//失败
打开文件失败时,比较流,如
if(mystream==false)
{
//File wasn't opened properly
throw openFailed
}
如果检测到流为false,则抛出您自己编写的异常。但是Google上可访问的资源中没有一个能够告诉您在打开文件时是否会抛出错误。@JeroenBollen:文档中说将设置“fail”位。问题是我需要异常对象,因为我正在多线程处理。