"9.18" Write a program that is used to count the number of words in a text file.
#include <iostream>#include <fstream>using STD::Cin;using STD::cout;using STD:: Endl;using STD:: iOS;using STD:: Ofstream;using STD:: Ifstream;classfile{ Public: File (); ~file ();intSearch_is ();Private: Ofstream fout1; Ifstream Fout2;}; File::file ()//Create a text file{Ofstream FOUT1 ("Text1.txt", ios::out);if(!FOUT1) {cout<<"Cannot open output file."; System"Pause");Exit(1); } fout1 <<"He has a girlfriend and he's a boy.";//Enter the contents of the text fileFout1.close (); }file::~file () {fout2.close ();}intFile::search_is ()//Open Text file, look for is, statistics is{intF0 =0, fi =0, FS =0;intsum =0;CharCh Ifstream Fout2 ("Text1.txt", ios::in);if(!FOUT2) {cout<<"Error open file."; System"Pause");Exit(2); }//Is the word is the condition is: I before is a space, I after is S, and s after is a space while(ch = fout2.get ())! = EOF) {if(ch=="')//If it is a space{if(fs = =1)//If it is before a space,{++sum; } F0 =1;//1 indicates a spaceFS =0;//Keep fs to 0;}Else{if(f0==1&&ch==' I ')//If I is preceded by a space{fi =1; }Else if(fi==1&&ch==' s ')//If it is s, preceded by a space and I;{fs =1; fi =0; }Else if(fs==1)//If there are spaces before and I and S, but s is not a space{fs =0; } F0 =0;//reset F0;} }cout<<"is the number of:"<< sum <<"a"<< Endl;return 0;}intMain () {File fi; Fi.search_is (); System"Pause");return 1;}
Question:
I feel weird about this class I'm building, and I want to know how to build it: write the content into the text first, and then the class that handles the text, especially about the constructor of the class.
The 9th Chapter "C + + input and output:: Exercise" 9.18 "