1#include <stdio.h>
2#include <stdlib.h>
3
4int main(int argc, char * argv[])
5{
6 // This program reads a file from its arguments and prints a word by word. Additionally, it counts the words in the file.
7 if (argc < 2) return 1;
8 char * filename = argv[1];
9 FILE * fp = fopen(filename, "r");
10 if (fp == NULL) return 1;
11 char c;
12 int count = 0;
13 while((c = fgetc(fp)) != EOF)
14 {
15 if(c == ' ' || c == '\n')
16 {
17 printf("\n");
18 ++count;
19 }
20 else
21 {
22 printf("%c", c);
23 }
24 }
25 fclose(fp);
26
27 printf("This file has %d words in it.", count);
28 return 0;
29}