/*
Exercise 2 chapter 14
Write a program to count the number of characters and numbers of word in a file ;
*/
/*
Author : Guo Junling
Date : 2005.4.12
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std ;
int main()
{
//以输入的方式打开文件
ifstream in("Exercise3.txt", ios::in) ;
//如果打开失败,给出提示信息
if( in.fail() )
{
cout << " Fail on opening file " << endl ;
}
//ch_count 存储字符个数 , word_count 存储字符串信息,初始化为0
int ch_count = 0 , word_count = 0 ;
// ch_In_File 临时存储从文件读来的字符
char ch_In_File ;
//不断从文件中读取字符
while( in.get( ch_In_File ) )
{
ch_count++ ;
}
//关闭文件
in.close() ;
ifstream word ( "Exercise3.txt" , ios::in ) ;
//word_File 临时存储从文件读来的字符串
string word_File ;
//不断从文件中读取字符串
while( in >> word_File )
{
word_count++ ;
}
//分别输出字符和字符串的个数
cout << " No. of char in file : " << ch_count << endl ;
cout << " No. of string in file : " << word_count << endl ;
word.close() ;
return 0 ;
}