getopt() problems.......

This is a discussion on getopt() problems....... within the C Programming forums, part of the General Programming Boards category; Hi there.... I'm trying to become more framiliar with structured programming. I have a problem with an encryption program I'm ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    Post getopt() problems and Structured programming

    Hi there....
    I'm trying to become more framiliar with structured programming. I have a problem with an encryption program I'm creating.....
    I'm allowing command line arguments to be passed to main(), and using getopt() to allow the dash(-) option and argument following the option...
    I'm about 1/4 of the way finished, it compiles fine, but when I run it, I can not specify arguments with correct results... If you could, if it's not too much of a hasle, please compile/download attatchment....
    Thanks in advance.
    Here is my code so far:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    FILE *in;
    
    char usage(char *name); 
    char infile(char *arg1);
    
    main(int argc, char *argv[]) {
    
    	char c;
    
    
    	if (argc < 4) {
    		usage(argv[0]);
    		return (1);
    	}
    
    	
    	while((c = getopt(argc, argv, "i:o:k:")) != EOF) {
    
    	switch (c) {
    
    		case ('i') : infile(argv[1]);
    				break;
    	}
    		 
    	
    		
    	}
    	
    
    }
    
    char usage(char *name) {
    
    	printf("Usage:\t%s -i [infile] -o [outfile] -k [key]\n", name);
    	return;
    }
    
    char infile(char *arg1) {
    
    	if ((in = fopen(arg1, "r")) == NULL) {
    		printf("There was an error opening \"%s\"\n", arg1);
    		exit(1);
    	
    	}
    
    }
    Attached Files Attached Files
    Last edited by tyskater; 10-24-2003 at 11:43 PM.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    I've never used getopt(), and it's not standard.

    The way I get the command-line arguments is simply:
    Code:
    int parm = 1;
    while (parm < argc)
    {
        if (argv[parm][0] == '-') 
        {           // it's a switch
            switch (toupper(argv[parm][1]))
            {
                case 'I':        // input switch?
                    break;
                case 'O':        // output switch?
                    break;
            }
        }
        else
        {           // it's not a switch
         /* do something else */
        }
        parm++;     // next parameter
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    case ('i') : infile(optarg);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21