Sample – count number of characters and lines in a file

by Nideesh C on February 8, 2011 · 0 comments

in C++




#include <iostream.h>
#include <fstream.h>

int main () {
ifstream f1;
char c;
int numchars, numlines;

f1.open(“test”);

numchars = 0;
numlines = 0;
f1.get(c);
while (f1) {
while (f1 && c != ‘\n’) {
numchars = numchars + 1;
f1.get(c);
}
numlines = numlines + 1;
f1.get(c);
}
cout << “The file has ” << numlines << ” lines and ”
<< numchars << ” characters” << endl;
return(0);
}

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Sample program (multiplication using addition)
  2. Stereo FM Transmitter Using I.C – BH1417
  3. Little about system file types

Leave a Comment

Previous post:

Next post: