1
0
Fork 0
This repository has been archived on 2021-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
uni-m.cds-adv-prog/ex5.3/main.cpp
Eric Teunis de Boone a54f03b178 Ex5: Input Streams
2019-12-17 18:05:47 +01:00

52 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;
}