简体   繁体   English

扫描参数并使用C ++程序确定参数

[英]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". 我们必须创建一个“在UNIX / Linux中称为过滤器的通用应用程序编程模型”。

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: 例如, cmd已打开并输入以下行:

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? scanf是我要用的吗? My main is set up to be able to accept arguments: 我的main对象设置为能够接受参数:

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 . 您需要使用getopt The manual page has an example. 手册页上有一个例子。

argv is an array of strings. argv是字符串数组。 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> 一旦有了特定参数的字符串,就可以使用<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. 除非您的任务只是处理参数,否则您可能要查找getopt这是用于参数的标准库解析器。

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. 至于问题的实质,有很多选择,您可以将sscanf用作其中的一部分,但不必这样做。

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. 要解析您提到的一个参数,您需要执行以下操作:检查参数是否以-i开头,从参数中获取数据。

The easiest way to check if the argument begins with -i is: 检查参数是否以-i开头的最简单方法是:

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. 注意,我使用的是argv [ 1 ],而不是0。argv [0]始终是可执行文件的名称。

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. 这是最有效的-重用argv中已经存在的字符串。 If you're planning on changing the strings around, you'd do better with strcpy: 如果您打算更改字符串,最好使用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"). argv获取参数(“参数矢量”)。 In your example, argc and argv will be as follows: 在您的示例中, argcargv将如下所示:

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

Finding the first letter after the hyphen in argv[1] is a simple loop. argv[1]找到连字符后的第一个字母是一个简单的循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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