/* mywc.cpp
Counts number of lines, words and characters in an ascii file.
This is a basic form of the wc command in linux.
Usage: mywc <file>
October 2009
*/
#include <iostream>
#include <fstream>
using namespace std;
#define CR '\n'
#define TAB '\t'
#define SPACE ' '
#define ZERO 0U
int main( int argc, char *argv[] )
{
if(argc != 2){
cout << "Missing or too many parameters." << endl;
cout << "Usage: mywc <file>" << endl;
return -1;
}
ifstream source( argv[1] );
// Check if files can be opened
if (source.is_open()==false){
cout << "Unable to open file " << argv[1] << endl;
return 1;
}
bool flag = false;
unsigned long character, word, line;
char ch;
character = word = line = ZERO;
while( !(source.eof()) )
{
// get a single character
source.get(ch);
// count number of characters
character++;
// count newlines
if( ch == CR ) line++;
// count words
if ( ch == SPACE || ch == CR || ch == TAB )
flag = false;
else if(flag == false){
flag = true;
word++;
}
}
source.close();
cout << line-1 << SPACE << word << SPACE << character -1 << TAB << argv[1] << CR;
return 0;
}