For Exercise 8.6
1 //ConsoleApplication10.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6#include <fstream>7#include <sstream>8 9 using namespacestd;Ten One A voidPrint (IStream &inch) - { - stringstr; the while(inch>>str) - { -cout << str <<Endl; - } + } - + intMain () A { atIfstreaminch("D://hello.txt"); -Printinch); - inch. Clear (); - inch. Close (); - return 0; -}
For Exercise 8.7
1#include"stdafx.h"2#include <iostream>3#include <fstream>4#include <sstream>5#include <vector>6 7 using namespacestd;8 9 intMain ()Ten { Onevector<string>Vect; AVect.push_back ("D://hello0.txt"); -Vect.push_back ("D://hello.txt"); -Vect.push_back ("D://hello1.txt"); theVect.push_back ("D://hello2.txt"); - for(vector<string>::iterator begin = Vect.begin (); Begin! = Vect.end (); ++begin) - { -Ifstreaminch; + inch. Open (begin->c_str ()); - if(!inch) + { Acout << begin->c_str () <<"does not exist in the system!!!"<<Endl; at - Continue; - } -cout <<"opening the file with name is:"<< begin->c_str () <<Endl; - stringstr; - while(inch>>str) in { -cout << str <<Endl; to } + inch. Clear (); - inch. Close (); the } * return 0; $}
<<c++ primer>> Fourth Edition exercise section 8.4.1 exercises