打开文件--------程序

 1 //打开文件
 2 #include <iostream>
 3 #include<fstream>
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9 
10     int a,b;
11     ifstream inData;
12 
13     inData.open("loan.txt");
14     if (!inData)
15     {
16         cout << "不能打开!!!" << endl;
17         return 1;                               //如果打开失败,终止程序;否则,打开空文件赋值随机
18     }
19 
20     inData >> a >> b;                           //赋值
21     cout << a+4+b;
22 
23     inData.close();
24 
25     cin.ignore();
26     cin.get();
27     return 0;
28 }

txt文件内容:

3
2

输出:

9

posted @ 2017-06-26 19:29  sky_lover  阅读(189)  评论(0编辑  收藏  举报