// License #include #include #include #include #include void usage(char* exeName) { fprintf(stderr, "Usage: %s file\nPrints out the number of lines, words, and characters in the file\n", exeName); exit(1); } void fileError(char* filename) { fprintf(stderr, "Some error reading file '%s'\n", filename); exit(2); } int main(int argc, char* argv[]) { if ( argc != 2 ) usage(argv[0]); typedef enum {IN_WS, IN_WORD} StateType; StateType state = IN_WS; FILE* f = fopen(argv[1], "r"); if ( f == NULL ) fileError(argv[1]); uint64_t chars, words, lines; char c; while ( (c = fgetc(f)) != EOF ) { chars++; if ( c == '\n' ) lines++; switch (state) { case IN_WS: if ( !isspace(c) ) { words++; state = IN_WORD; } case IN_WORD: if ( isspace(c) ) { state = IN_WS; } } } if ( 0 != fclose(f) ) { perror("fclose"); exit(3); } printf(" %" PRIu64 " lines, %" PRIu64 " words, %" PRIu64 " characters\n", lines, words, chars ); }