The 9th Chapter "C + + input and output:: Exercise" 9.18 "

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.