C++ Program to Count Number of Words, Lines and Total Size of a Text File Full Project For Beginners

  • Post author:
  • Post category:c++
  • Post comments:0 Comments

 

 

main.cpp

 

 

#include<iostream.h>
#include<fstream.h>
 
int main()
{
ifstream fin("story.txt"); //opening text file
int line=1,word=1,size; //will not count first word and last line so initial value is 1
char ch;
fin.seekg(0,ios::end); //bring file pointer position to end of file
size=fin.tellg(); //count number of bytes till current postion for file pointer
fin.seekg(0,ios::beg); //bring position of file pointer to begining of file
while(fin)
{
  fin.get(ch);
  if(ch==' '||ch=='n')
   word++;
  
  if(ch=='n')
   line++;
}
cout<<"Lines="<<line<<"nWords="<<word<<"nSize="<<size<<"n";
fin.close(); //closing file
return 0;
}

Leave a Reply