<<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 }