简体   繁体   中英

Scan arguments and determine what they are with a c++ program

This is a homework assignment first off.

We have to create a "common application-programming model in UNIX/Linux known as the filter".

I'm stuck on reading the input passed through as arguments (it's all I ever seem to have trouble on).

For example, the cmd is open and the following line is entered:

program -isomebinaryfile.bin

I need to determine what the first letter is after the hyphen ( - ) and so on and so forth.

Is scanf what I would be using? My main is set up to be able to accept arguments:

int main (int argc, char *argv[])
{
FILE *inf = NULL;
char *arg = argv[0];
}

Can someone give me a little help?

You need to use getopt . The manual page has an example.

argv is an array of strings. You can loop over them like

for (int i=1; i<argc; i++) { // skip argv[0] as that's this program's name
    const char* arg = argv[i];
}

Once you have the string for a particular argument, you can use the string manipulation functions from <string.h>

if (arg[0] == '-' && strlen(arg) > 0) {
    arg++; // advance past the leading '-'
    if (strcmp(arg, "command_one") == 0) {
        // handle command_one
    }
    else if (strcmp(arg, "command_one") == 0) {
        ....
    else {
        printf("Error: unexpected command %s\n", arg);
    }

Unless your assignment is only to handle processing of arguments, you may want to look up getopt - it's a standard library parser for arguments.

As for the meat of your question, there are a lot of options, and you could use sscanf as part of it, but you don't have to.

To parse the one argument you mentioned, you need to do the following: check if the argument begins with -i, grab the data out of the argument.

The easiest way to check if the argument begins with -i is:

if (argv[1][0] == '-' && argv[1][1] == 'i')

Alternatively, if you have a lot of argument options, all beginning with '-', you may want something like:

char * i = NULL;
char * o = NULL;
char * s = NULL;
for (int i = 1; i < argc; ++i) {
    if (argv[i][0] == '-') {
        switch(argv[i][1]) {
            case 'i':
                i = argv[i][2];
                break;
            case 's':
                s = argv[i][2];
                break;
            case 'o':
                o = argv[i][2];
                break;
            default:
                cerr << "Unknown option: " << argv[i][1];
        }
     } else {
        cerr << "Error: all options must begin with '-'";
     }

Note, I'm using argv[ 1 ], not 0. argv[0] is always the name of the executable.

The fastest way to extract the rest of the argument is simple pointer arithmetic:

char * filename = argv[1] + 2;  // (Or you could equivalently say = &argv[1][2]

This is most efficient - it reuses the strings that are already in argv. If you're planning on changing the strings around, you'd do better with strcpy:

char * filename = (char *)malloc(strlen(argv[1]) - 2);
strcpy(filename, argv1 + 2);
// and eventually you'd have to free(filename)...

Play around and experiment with all the string functions. You'll find them essential to all of your later programs.

Get arguments from argv ("argument vector"). In your example, argc and argv will be as follows:

argc == 2
argv[0] == "cmd"
argv[1] == "-isomebinaryfile.bin"

Finding the first letter after the hyphen in argv[1] is a simple loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM