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

Recommended Answers

All 5 Replies

You can loop through argc and read values stored in argv[].

for (i = 1 to argc) {
   read file argv[i]
}

You could do something like that.
argc, argv

You can create a loop using the argc argument. Supposing you want to start from argument #1:

for (int i = 0; i<=argc; i++)
{
   //Your code goes here
}

i have tried that but it is still only doing it for the first file...

#include <stdio.h>




   int main (int argc, char *argv[])
   
   { 
   	int i;
      for (i = 0; i<=argc; i++)
      
      {
      
         int cur;
         int countLn = 0;
         int countCh = 0;
         int countWd = 0;
         FILE *fp1;
      
      	fp1 = fopen(argv[i+1], "r");
         if (!(fp1))
         {
            printf("Error in opening", argv[i+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;
      } 
   }

Have the return 0; statement outside the for loop.
If you do not wish to count the words of each file seperately, you will also need to modify your code accordingly.

Have the return 0; statement outside the for loop.
If you do not wish to count the words of each file seperately, you will also need to modify your code accordingly.

thanks...i knew it was something simple :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.