c++读文件-对try-throw-catch的应用

1 #include<iostream>
 2 #include<fstream>
 3 #include<stdlib.h>
 4 #include<stdio.h>
 5 using namespace std;
 6 int main(int argc, char** argv)
 7 {
 8     ifstream file(argv[1]);    
 9     char line[100] = {0};
10 
11     try
12     {
13         if (file.fail())   //如果提供一个不存在的路径就会出错
14             throw argv[1];
15     }
16     catch (char* s)
17     {
18         cout<<"open file:["<<argv[1]<<"] failed"<<endl;
19         exit(1);
20     }
21 
22     while (!file.eof())
23     {
24         file.getline(line, sizeof(line)/sizeof(char)); //读取文件每一行,直到文件结束
25         cout<<line<<endl;
26     }
27 
28     file.close();  //切记关闭文件啊.
29 
30     return 0;
31 }

原文链接: https://www.cnblogs.com/silentNight/p/5560785.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/234625

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午4:20
下一篇 2023年2月13日 下午4:21

相关推荐