简体   繁体   English

C ++,命令行,参数

[英]C++, command line, parameters

I am starting my c++ program from command line: 我正在从命令行启动我的C ++程序:

program input_file1 input_file2 output_file

where 哪里

int main( int argc, char *argv[] )
{
    short indicator= 3;
    char input_file1[4096], input_file2[4096], output_file[4096];
    char *p_file = NULL;
    while ( --argc > 0 ) {
        switch (--indicator) {
          case 2:
            p_file = output_file;
            break;
          case 1:
            p_file = input_file2;
            break;
          case 0:
            p_file = input_file1;
            break;
        }

        for (char *argument = argv[argc]; ; ++argument) {
            if (*argument == '\0')
                break;
            else
                *p_file++ = *argument;
        }

        *p_file = '\0'; 
    }

    std::cout << input_file1 << '\n';
    std::cout << input_file2 << '\n';
    std::cout << output_file << '\n';
}

But with the real arguments 但是真正的争论

program D:\\data\\file1.txt D:\\data\\file2.txt D:\\data\\file3.txt

in names of the files only the first letter D is stored... 在文件名中,仅存储第一个字母D ...

Output:
D
D
D

Thanks for your help... 谢谢你的帮助...

Ok, so here is the short version: 好的,这是简短的版本:

int main(int argc, char *argv[]) {
    if (argc != 2) {
        std::cout << "This program requires 1 argument!" << std::endl;
        return 1;
    }
    std::string input_file(argv[1]);
    std::cout << input_file << std::endl;
}

You should be able to take it from here. 您应该可以从这里拿走它。

This is a C problem, not a C++ one, but as it is tagged C++, i will suggest a C++ solution for your problem : 这是一个C问题,而不是C ++问题,但由于它被标记为C ++,因此我将为您的问题建议一个C ++解决方案:

int main( int argc, char *argv[] ) {
     std::vector<std::string> args(argv+1, argv+argc);
     std::cout << args[0] << '\n' << args[1] << '\n' << args[2] << std::endl;
}

UPDATE using iterators on argv to fill the vector args (thanks Space_C0wb0y) 使用argv上的迭代器进行更新以填充矢量args(感谢Space_C0wb0y)

Rather than copying the arguments, just set the file names to point to the appropriate entry in argv. 无需复制参数,只需将文件名设置为指向argv中的相应条目即可。

int main(int argc, char *argv[]) {
    char *input_file1, *input_file2, *output_file; 

    if (4 > argc)
    {
        std::cout << "Not enough parameters\n";
        return -1;
    }
    input_file1 = argv[1];
    input_file2 = argv[2];
    output_file = argv[3];

    std::cout << input_file1 << '\n';
    std::cout << input_file2 << '\n';
    std::cout << output_file << '\n'; 
}

the contents of argv will exist for as long as the program is running. 只要程序正在运行,argv的内容就会存在。

*p_file ++ = * argument; 

This assigns the first character of arguement to the first character in p_file. 这会将争辩的第一个字符分配给p_file中的第一个字符。

You need to use strcpy , or declare some std::strings instead of arrays of char 您需要使用strcpy ,或声明一些std::strings而不是char数组

Your loop is all wrong. 您的循环全错了。 You are looping through characters, not the parameter array. 您正在遍历字符,而不是参数数组。 You can do that like this: 您可以这样做:

for(auto arg = argv + 1; *arg; ++arg)
{
    std::cout << (*arg) << '\n'; // *arg is a char*
}

This works because the arguments (if any) start at argv + 1 and because they are null terminated so that *arg is nullptr (converts to false ) at the end of the argument array. 之所以argv + 1 ,是因为参数(如果有的话)从argv + 1开始,并且因为它们以null终止,因此*arg在参数数组的末尾为nullptr (转换为false )。

Your arguments are obtained by dereferencing arg using *arg which is a char* . 您的参数是通过使用*arg它是char*解引用arg来获得的。

So you can do: 因此,您可以执行以下操作:

if(!std::strcmp(*arg, "--help"))
    print_help_info();

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

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