Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Scanning for Options in C

Name: Anonymous 2010-10-29 4:12

Hi guys. I was wondering what the most common way to check for options, declared with '-' at the command line, is in C. For example: prog -a filename would run the program prog with the -a option. I assume it involves scanning with getopt. Thanks.

Name: Anonymous 2010-10-29 6:17

Hi >>3-8. After doing some research and reading a good explanation authored by IBM located here:

http://www.ibm.com/developerworks/aix/library/au-unix-getopt.html

I got it working. Now, I have a new question that I can't seem to figure a clear way around: My program doesn't distinguish between options and filenames, when looking for filenames to scan with fopen(). For example:

prog -a testfile

will properly set the -a option and function as intended, but instead of then accessing testfile, it will first attempt to open a file named -a, which of course does not exist. What would be the best way to circumvent this? All commands are preceded with a minus symbol, and all commands set a bool in a global argument storage structure as in the IBM tutorial, i.e.

struct globalArgs_t {
    int noIndex;                /* -I option */
    char *langCode;             /* -l option */
    const char *outFileName;    /* -o option */
    FILE *outFile;
    int verbosity;              /* -v option */
    char **inputFiles;          /* input files */
    int numInputFiles;          /* # of input files */
}


Thanks for taking the time to read this post.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List