Thread: command line arguments question

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

    command line arguments question

    Hey guys i need to check that exatcly 4 arguments are entered via the command line but i if i pass more or only 3 command line arguments it runs when i want it to sto executing.

    And would the if statements == NULL be a good way of checking whether the files exist and if not the case exit the program?

    Could someone double check if iv done these correctly?



    Code:
    int commandLineArguments(int argc, char **argv) 
    {   
        /* makes a local copy of argc and **argv */
    
     
       FILE *customer, *stock, *logfile; 
       
        /* declares a pointer to each file*/
    
        /* test if 4 arguments exist including the exucutable*/
    
       if(argc !=4) 
       {
         printf("please enter 3 cmd arguments!\n");
         return 1;  
         /* stop pocessing*/
       }
    
        /* attempt to open and read arg 1*/
    
       customer = fopen(argv[1], "r");
    
        /* check if custome.csv exists*/
    
       if(customer ==NULL)
       {
         printf("cannot open %s for reading\n", argv[1]);
         return 1;  
         /* can't go on*/
       }
    
         /* try to open stock.csv and read it*/
    
       stock = fopen(argv[2], "r");
      
         /* if stock.csv does not exist error*/
       
       if(stock == NULL)
       {
         printf("cannot open %s for reading\n",argv[2]);
         return 1;  
    
         /* stop processing*/
       }
    
         /* open logfile for reading */  
     
       logfile = fopen(argv[3], "r");
    
         /* check if logfile exists*/
    
       if(logfile == NULL)
       {
         printf("cannot open %s for reading\n", argv[3]);
         return 1;  
     
         /* stop processing*/
       }
       
         /* close all files */
    
         /* checking for closing file errors */
    
       if(fclose(customer)!=0 || fclose(stock) !=0 || fclose(logfile) !=0)
       {
          fprintf(stderr, "Error in closing files\n");
       }
    
       return 0;
    
        
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    if(argc !=5)  // for 4 arguments ( argv[0] is the name of the executable )
    and yes to check if the FILE * returned by fopen() is NULL is normal
    Kurt
    Last edited by ZuK; 09-29-2005 at 08:23 AM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int commandLineArguments(int argc, char **argv)
    main()?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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