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

56 lines
975 B
C++
Raw Normal View History

2019-12-17 18:05:41 +01:00
#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];
// 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 ) {
std::string linebuf;
2019-12-17 18:05:41 +01:00
line >> linebuf;
if ( ! linebuf.empty() ) {
wordcount++ ;
}
2019-12-17 18:05:41 +01:00
if ( line.eof() )
{
break ;
}
}
}
std::cout << " " << linecount-1 << " " << wordcount << " " << charcount-1 << " " << argv[1] << std::endl;
2019-12-17 18:05:41 +01:00
}