小编cap*_*tus的帖子

尝试捕获异常处理C++

我刚刚开始在C++中使用trycatch块进行异常处理.我有一个带有一些数据的文本文件,我正在使用ifstream和阅读此文件getline,如下所示,

ifstream file;
file.open("C:\\Test.txt", ios::in);
string line;
string firstLine;
if (getline(file, line, ' '))
{
    firstLine = line;
    getline(file, line);
}
Run Code Online (Sandbox Code Playgroud)

我想知道如何实现异常处理的情况下,file.open无法打开指定的文件,因为它不会在给定的路径存在,例如没有Test.txtC:

c++ exception-handling ifstream

1
推荐指数
1
解决办法
9710
查看次数

标签 统计

c++ ×1

exception-handling ×1

ifstream ×1