53 lines
972 B
C++
53 lines
972 B
C++
|
#include <fstream>
|
||
|
#include <iostream>
|
||
|
#include <cstring>
|
||
|
#include <sstream>
|
||
|
|
||
|
int main( int argc, char* argv[] ) {
|
||
|
|
||
|
|
||
|
if ( argc != 2) {
|
||
|
std::cout << "usage: " << argv[0] << " FILENAME" << std::endl;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
std::ifstream fh(argv[1]);
|
||
|
|
||
|
if ( !fh ) {
|
||
|
std::cout << "Error opening File '" << argv[1] << "'" << std::endl;
|
||
|
return 2;
|
||
|
}
|
||
|
|
||
|
unsigned int linecount = 0;
|
||
|
unsigned int charcount = 0;
|
||
|
unsigned int wordcount = 0;
|
||
|
const int bufsize = 100;
|
||
|
char buf[bufsize];
|
||
|
|
||
|
const int linebufsize = 10;
|
||
|
char linebuf[linebufsize];
|
||
|
|
||
|
// if EOF, fh returns false
|
||
|
while( fh ) {
|
||
|
fh.getline(buf, bufsize);
|
||
|
|
||
|
charcount += strlen(buf)+1; // Newlines are not counted normally, but they are a character
|
||
|
linecount++;
|
||
|
|
||
|
std::istringstream line(buf);
|
||
|
while( line ) {
|
||
|
line >> linebuf;
|
||
|
wordcount++ ;
|
||
|
|
||
|
if ( line.eof() )
|
||
|
{
|
||
|
break ;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
std::cout << " " << linecount-1 << " " << wordcount-1 << " " << charcount-1 << " " << argv[1] << std::endl;
|
||
|
}
|