|
-
October 27th, 2010, 08:55 PM
#1
Simple Counting Program
Hey guys, I'm trying to create a program that reads from a text file "mytext.txt" and outputs on the screen the number of lines, characters, and words in the file. I have the lines portion working, but can't think of a way to get each character individually, and words?
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main()
{
char line[100];
ifstream infile;
infile.open("U://mytext.txt");
if (infile.fail())
{
cout<<"File not found! Exiting."<<endl;
exit(0);
}
int characters=0, lines=0, words=0;
while(infile.getline(line, 100))
{
lines++ ;
}
cout<<"The total number of lines in the file is: "<<lines<<endl;
infile.clear() ;
infile.seekg(0, ios::beg) ; //so that the beginning of the file is being read again
while(infile)
{
//not sure what to do here?
characters++ ;
}
cout<<"The total number of characters in the file is: "<<characters<<endl;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|