sta*_*o14 9 c parameters program-entry-point mingw wildcard-expansion
我写了一个必须显示主要参数的代码,但是当我编译它并输入“*”时,程序显示了我的文件结构。cmd 中的命令如下所示:program.exe 1 2 3 *
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const* argv[]) {
    for (int i=0; i<argc; i++) printf("%s\n", argv[i]);
    return 0;
}
结果是:
program
1
2
3
program.c
program.exe
10-03-20
11-02-20
我的问题:是否可以强制程序打印“*”而不是列出文件。
mingw 使程序执行参数的通配符扩展。将以下内容添加到您的程序以禁用此行为:
int _CRT_glob = 0;
在 unix 世界中,shell 有望执行通配符扩展。
$ perl -le'print for @ARGV' *
a
b
在 Windows 世界中,通配符扩展留给应用程序。
>perl -le"print for @ARGV" *
*
That makes writing portable programs tricky. Since mingw is often used to compile programs that weren't written with Windows in mind, its C runtime library performs wildcard expansion of the parameters automatically.
a.c:
#include <stdio.h>
int main(int argc, char const* argv[]) {
    for (int i=0; i<argc; i++)
        printf("%s\n", argv[i]);
    return 0;
}
>gcc -Wall -Wextra -pedantic-errors a.c -o a.exe & a *
a
a.c
a.exe
But, mingw provides an out. Adding the following to your program disables this behaviour:
int _CRT_glob = 0; 
a.c:
#include <stdio.h>
int _CRT_glob = 0; 
int main(int argc, char const* argv[]) {
    for (int i=0; i<argc; i++)
        printf("%s\n", argv[i]);
    return 0;
}
>gcc -Wall -Wextra -pedantic-errors a.c -o a.exe & a *
a
*