Hi there i am trying to make a program that takes multiple files from the command line.
I think i have got my my word count working right, but my question is how do i get it to read multiple files on the command line. Like when i do
./a.out wordcount.c file1 file2
heres my code any help would be appreciated
#include <stdio.h>
#define Space (cur == ' ' || cur == '\n' || cur == '\t')
int main (int argc, char *argv[])
{
int cur;
int preCh;
int countLn = 0;
int countCh = 0;
int countWd = 0;
FILE *fp1;
if (!(fp1 = fopen(argv[1], "r")))
{
printf("Error in opening", argv[1], "for reading");
return (1);
}
while ((cur = fgetc(fp1)) != EOF)
{
if (cur != '\n')
countCh++;
if (cur == ' ')
countWd++;
if (cur == '\n')
countLn++;
}
printf("\n");
printf("Number of characters: %d\n", countCh);
printf("Number of lines: %d\n", countLn);
printf("Number of words: %d\n", countWd);
printf("\n");
fclose(fp1);
return 0;
}