Thread: getopt option arguments

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    70

    getopt option arguments

    I am having some trouble writing a program using getopt.

    How would i use getopt to accept arguments (2 filenames for each option)?

    For example: ./prog -a file.txt

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[]){
      int optchar;
      
    
      while((optchar = getopt (argc, argv, "nsvt")) != -1){
        switch(optchar){
        case 'n':
          
          FILE *fp;
          fp = fopen("text.txt, "r");
          if(fp == NULL){
            printf("File couldn't be opened!\n");
          }
            else{
              printf("File opened!\n");
              int c;
              while((c = fgetc(fp)) != EOF){
                if(c == '\n'){
                  printf("newline found\n");
                }
                printf("%c", c);
              }
              fclose(fp);
            }
    
            break;
    
          case 's':
            printf("case opt s!\n");
            break;
    
          case 'v':
            printf("case opt v!\n");
            break;
    
          case 't':
            printf("case opt t!\n");
            break;
          }
        }
        return 0;

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    60
    you don't really get what option means, do you?
    option is one-to-one input, one option w/ one value.
    why do you need such of 2 values for one option?
    why not use one more extra option for other file?

    Code:
    ./program -s source_file.txt -d destination_file.txt

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    I am trying to emulate the cat command, so i want to use ./program -a file.txt file.txt

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    i use this template from here . Example of Getopt - The GNU C Library there is a long option also.

    this little program is how i would have done it as a hello world program but i am not sure if there is not a better more nice way of using it. i am still new to this.

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <errno.h>
    
    
    void PrintUsage(const char *progname);
    void fcopy(FILE *in,FILE *out);
    
    int  main (int argc, char **argv)
    {
           int aflg = 0;
           int bflg = 0;
           char *cval = NULL;
           int index;
           int c;
           const char *progname=argv[0];
           opterr = 0;
           FILE *fp;
    	   
           while ((c = getopt (argc, argv, "abc:")) != -1)
           {     switch (c)
                 {
                        case 'a': aflg = 1;       break;
                        case 'b': bflg = 1;       break;
                        case 'c': cval = optarg;  break;
                        case '?':
                           if (optopt == 'c')
    			{
                               fprintf (stderr," "
                                          "Option -%c requires an argument.\n"
    					  " ", optopt);
                           }
    			else if (isprint (optopt))
    		      {
                               fprintf (stderr, "Unknown option `-%c'.\n", optopt);
                          }
    		      else
    		     {
                               fprintf (stderr,
                                       "Unknown option character `\\x%x'.\n",
                                       optopt);
                               exit(EXIT_FAILURE);
    		    }
    					 
                       default:
                       abort (); 
                 }    
            }
    		
               if(aflg)
    	        for (index = optind; index < argc; index++)
    	       {
                
    			      fprintf(stdout,"Non-option argument %s\n", argv[index]);
    				  exit(0);
    	       }
    		   else if(bflg)
    		   {
    			   
                       fprintf(stdout,"aflag = %d, bflag = %d, cvalue = %s\n",
                                aflg, bflg, cval);
    							exit(0);
               }
    		   
    		   else if(cval != NULL)
    		   {
    		     printf("opening file %s \n",cval);
    			 if((fp=fopen(cval,"r")) == NULL)
    			 {
    				     exit(1);
    			 }
    			 else
    			 {
    			     
    			 	  fcopy(fp,stdout);
    		          exit(EXIT_SUCCESS);
                 }
    		   }
    		   
    	       else
    	       {
    	         fprintf(stdout,"%s executing :no options\n",progname); 
    	       }
    	   
           return 0;
    }
    
    void PrintUsage(const char *progname)
    {
    	 fprintf(stdout,"USAGE:%s [options]\n"
    	                "barebones usage help text\n",
    			 progname);
    }
    
    void fcopy(FILE *in,FILE *out)
    {
    	int c;
    	while((c=getc(in)) != EOF)
    	      fprintf(out,"%c",c);
    }
    option a shows arguments giving
    option b shows what flags status is
    option c needs as argument which is on thsi case teh file name to open and display on screen.

    i made the example slight modified from the page i linked to and use it as a template in my editor.-

    ps:sorry if the indent goes when i post. i have indented before i posted. i hope it is working.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    I am very sorry, i didn't read about optarg() properly. I was trying to use optarg when what i wanted was non option args for reading 2 files and printing them to stdout.

    Thanks cmay.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM