简体   繁体   English

C++ 控制台中的星号列出了文件夹中的所有文件,为什么?

[英]Asterisk in C++ console lists all files in folder, why?

So I wanted to create a simple console calculator that'd take arguments via the int main(int argc, char ** argv) function.所以我想创建一个简单的控制台计算器,通过int main(int argc, char ** argv) function 获取 arguments。 It works for regular examples, but trying multiplication with '*' started giving me way too many arguments listed in argv[], so I tinkered around and figured out it was listing all the files in the folder when '*' was used, supposedly for multiplication.它适用于常规示例,但尝试与 '*' 相乘开始给我太多的 argv [] 中列出的 arguments,所以我修改了一下,发现它在使用 '*' 时列出了文件夹中的所有文件,据说为乘法。 I turned the if statement to always be true for debugging.我将 if 语句设置为始终适用于调试。 I didn't find anything wrong with cerr , although I could've used the try-catch block.尽管我可以使用 try-catch 块,但我没有发现cerr有任何问题。

I looked around the internet to see the reason for that, and I found that the asterisk can be used as some sort of wildcard to look for files in the folder, pretty similar, but not exactly what I was looking for.我在互联网上查看了原因,我发现星号可以用作某种通配符来查找文件夹中的文件,非常相似,但不完全是我想要的。 Thoughts?想法?

Source code:源代码:

#include <iostream>
#include <string>

using namespace std;
int main(int argc, char ** argv)
{
    cout << "Argument count: " << argc << endl;
    if(true) // argc != 4
    {
        int i(0), j(0);
        while(argv[i])
        {
            j = 0;
            while(argv[i][j])
            {
                cout << argv[i][j];
                ++j;
            }
            cout << endl;
            ++i;
        }
    }
        //cerr << "Inadequate amount of arguments! " << argc;
    else
    {
        double num1, num2;
        try
        {
            num1 = stoi(argv[1]), num2 = stoi(argv[3]);
        }
        catch(exception e)
        {
            cout << "Unable to transform to integer.\n";
            return -1;
        }
        char op = *argv[2];
        switch(op)
        {
            case '+': cout << num1 + num2; break;
            case '-': cout << num1 - num2; break;
            case '*': cout << num1 * num2; break;
            case '/': cout << num1 / num2; break;
            default: cout << "Invalid operator.";
        }
    }

}

This has likely nothing to do with C++ and everything to do with your shell.这可能与 C++ 和您的 shell 无关。

Bash and other sh-like shells allow you to use "glob" patterns to name multiple files easily. Bash 和其他类似 sh 的 shell 允许您使用“glob”模式轻松命名多个文件。 This allows you to, for example, easily move all.png files in a directory to another directory with a command like mv *.png some-other-dir .例如,这使您可以使用mv *.png some-other-dir类的命令轻松地将目录中的 all.png 文件移动到另一个目录。 The shell substitutes *.png for the names of all files in the current directory that end in ".png". shell 将*.png替换为当前目录中以“.png”结尾的所有文件的名称。

* is the "match anything" pattern, so a * in a command will expand to the names of all files in the current directory. *是“匹配任何东西”模式,因此命令中的*将扩展为当前目录中所有文件的名称。 That means that if you run ./my_program 3 * 4 in a directory containing the files "my_program", "foo.txt", and "bar.txt", your shell will expand the * character and actually run ./my_program 3 bar.txt foo.txt my_program 4 .这意味着如果您在包含文件“my_program”、“foo.txt”和“bar.txt”的目录中运行./my_program 3 * 4 ,您的 shell 将展开*字符并实际运行./my_program 3 bar.txt foo.txt my_program 4 .

You can disable this expansion by wrapping the * character in single quotes or escaping it with a \ .您可以通过将*字符用单引号或 escaping 用\括起来来禁用此扩展。 If you run ./my_program 3 '*' 4 or ./my_program 3 \* 4 , your shell will not expand the * , and argv will contain {"./my_program", "3", "*", "4"} .如果您运行./my_program 3 '*' 4./my_program 3 \* 4 ,您的 shell 将不会扩展* ,并且argv将包含{"./my_program", "3", "*", "4"}


Note that Windows CMD shell does not do this sort of expansion.请注意,Windows CMD shell 不会进行这种扩展。 If you want this behavior on Windows you would have to link your program to setargv.obj to get the runtime library to do it for you or implement the expansion yourself.如果您希望 Windows 上的这种行为,您必须将您的程序链接到 setargv.obj 以让运行时库为您执行此操作或自己实现扩展。

The asterisk * is expanded by your shell before your program is started.在您的程序启动之前,星号*由您的 shell 扩展。 There's nothing you can do in your program to change this behavior.您无法在程序中更改此行为。 With most popular shells like Bash, sh, zsh etc. you have to quote the arguments like so:对于 Bash、sh、zsh 等最流行的 shell,您必须像这样引用 arguments:

program '2*3'

or或者

program 2\*3

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

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