<<C++ Primer>> 第四版Exercise Section 8.4.1 练习题

For exercise 8.6

 1 // ConsoleApplication10.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include <fstream>
 7 #include <sstream>
 8 
 9 using namespace std;
10 
11 
12 void print(istream &in)
13 {
14     string str;
15     while (in >> str)
16     {
17         cout << str << endl;
18     }
19 }
20 
21 int main()
22 {
23     ifstream in("d://hello.txt");
24     print(in);
25     in.clear();
26     in.close();
27     return 0;
28 }

 

For Exercise 8.7

 1 #include "stdafx.h"
 2 #include <iostream>
 3 #include <fstream>
 4 #include <sstream>
 5 #include <vector>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     vector<string> vect;
12     vect.push_back("d://hello0.txt");
13     vect.push_back("d://hello.txt");
14     vect.push_back("d://hello1.txt");
15     vect.push_back("d://hello2.txt");
16     for (vector<string>::iterator begin = vect.begin(); begin != vect.end(); ++begin)
17     {
18         ifstream in;
19         in.open(begin->c_str());
20         if (!in)
21         {
22             cout << begin->c_str() << " does not exist in the system!!!" << endl;
23 
24             continue;
25         }
26         cout << "opening the file with name is: " << begin->c_str() << endl;
27         string str;
28         while (in >> str)
29         {
30             cout << str << endl;
31         }
32         in.clear();
33         in.close();
34     }
35     return 0;
36 }

转载于:https://www.cnblogs.com/lucy-lizhi/p/6506379.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值