块是否try-catch
捕获分段错误错误?
我正在使用下面给出的函数读取文本文件,但有时文件为空并且程序崩溃。我希望程序继续运行并在该文件为空或正在使用时提供另一个文件。
Path2D read_gesture(const char* filename)
{
Path2D path;
//MultiStrokeGesture MultiStrokes;
vector<string> text_file;
int no_of_paths=0;
std::ifstream ifs(filename);
for (std::string line; std::getline(ifs, line); )
{
no_of_paths=no_of_paths+1;
double a, b;
stringstream ss(line);
if (!(ss >> a >> b)) {cout<<"wrong format"<<endl;}
std::cout << "You said, " << a << ", " << b << ".\n";
path.push_back(Point2D(a,b));
}
cout<<"saving gesture"<<endl;
return path;
}
我试过类似的东西:
Path2D path;
try
{
path=read_gesture("test.txt");
}
catch(int e)
{
path=read_gesture("test2.txt");
}
但程序仍然崩溃。问题可能是什么?
- 稍微更正一下,调用的文件和 的文件
catch
不一样try
,是错别字。