Thread: getopt() problems.......

  1. #1
    Registered User
    Join Date
    Oct 2003

    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:
    #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) {
    		return (1);
    	while((c = getopt(argc, argv, "i:o:k:")) != EOF) {
    	switch (c) {
    		case ('i') : infile(argv[1]);
    char usage(char *name) {
    	printf("Usage:\t%s -i [infile] -o [outfile] -k [key]\n", name);
    char infile(char *arg1) {
    	if ((in = fopen(arg1, "r")) == NULL) {
    		printf("There was an error opening \"%s\"\n", arg1);
    Last edited by tyskater; 10-24-2003 at 11:43 PM.

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

    The way I get the command-line arguments is simply:
    int parm = 1;
    while (parm < argc)
        if (argv[parm][0] == '-') 
        {           // it's a switch
            switch (toupper(argv[parm][1]))
                case 'I':        // input switch?
                case 'O':        // output switch?
        {           // 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
    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, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05: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, 09:19 PM