Thread: command line arguments question

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    command line arguments question

    Hey guys im just playing around with this program which counts comments from source files such as .h or .c files. How would i pass command Line Arguments when i run the program.

    Iv done some passing but it dosnt seem to work im not sure if im passing the arguments correctly when i run it?


    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include "myprogexam.h"
    #include <string.h>
    #include <ctype.h>
    
    #define TRUE  1
    #define FALSE 0
    
    int main(int argc, char **argv)
    {
       int numComments;
    
       if(argc !=2)
       {
          printf("not enough arguments passed!\n");
          return EXIT_FAILURE;
       }
       if(argc >3)
       {
          printf("Too many arguments:\n");
          exit(0);
       }
    
       numComments = displayComments(argv);
    
       return EXIT_SUCCESS;
    }
    
    int displayComments(char **argv)
    {
       
       FILE *fp; /* fp point to a file*/
       int currentChar, previousChar;
       int notYetCommentLine, blanksOnlySoFar;
       int numComments, i;
       
       char * suffix[] = {".h", ".c", NULL};
       char * filename;
    
       /* allocate dynamic space for the file*/
       
       filename  = malloc(sizeof(char) * (strlen(*argv) + 3));
        
       /* check if the filename exists*/
    
       if(!filename)
       {
         printf("Something went wrong!\n");
         return -1;
       }   
       printf("hello\n");
       /* count the comments in each file*/
       
       for(i=0; suffix[i]; i++)
       {
          strcpy(filename, *argv);
          strcat(filename, suffix[i]);
    
          fp = fopen(filename, "r");
    
          if(fp)
          {
             previousChar = '\0';
             blanksOnlySoFar = TRUE;
             notYetCommentLine = TRUE;
    
             while((currentChar=fgetc(fp)) !=EOF)
             {
                if(currentChar == '/' && previousChar == '/' &&
                   blanksOnlySoFar && notYetCommentLine)
                {
                    notYetCommentLine = FALSE;
                    numComments++;
                    putchar('/');
                }
                else if (currentChar == '\n')
                {
                    if(!notYetCommentLine) putchar('\n');
                    notYetCommentLine = TRUE;
                    blanksOnlySoFar = TRUE;
    
                }
                else if (!isspace(currentChar) && (currentChar != '/'))
                {
    
                    blanksOnlySoFar = FALSE;
                }
    
                if (!notYetCommentLine) putchar(currentChar);
                
                previousChar = currentChar;
    
             }
             fclose(fp);
          }     
    
       }
       free(filename);
       return numComments;
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    How would i pass command Line Arguments when i run the program
    myprogram.exe <arg1> <arg2> <arg3> etc...

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    look, when u you say like this
    Code:
    strcpy(filename, *argv);
    u are basically reading the content of first argv[0] which is nothing but your on going file its self. if u understand buthib post

    myprogram.exe <arg1> <arg2> <arg3> etc...
    argv[0] [1] [2] [3]

    in your case if u say *argv its return u the same exe file rather than the parameter. the example program mighht explain u more

    Code:
    #include<stdio.h>
    
    int main(int argc,char  **argv)
    {
        printf("The no of argumenets %d\n",argc);
        
        printf("The argument that u reading : - %s\n",*argv);  // this is what u have done
        printf("The argumwnt that i have read : - %s %s\n",argv[0],argv[1]);
    
        getchar();
    }
    /*myoutput
    [d4088019@outcast ~/second]$ a.out hello.c
    The no of argumenets 2
    The argument that u reading : - a.out
    The argumwnt that i have read : - a.out hello.c
    */
    NOTE: no need of Strcat fucntion, b'cos u reading the file name with the extention itself

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command Line arguments question
    By micpi in forum C Programming
    Replies: 7
    Last Post: 04-24-2007, 08:46 PM
  2. question aboout arguments
    By only1st in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2007, 08:36 PM
  3. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM
  4. Easy question about arguments in a function
    By pond-person in forum C++ Programming
    Replies: 5
    Last Post: 12-12-2002, 10:36 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM