Windows實際上將整個原始命令行作爲單個字符串傳遞給程序;見GetCommandLine。當您編寫main(int argc, char **argv)
時,程序鏈接的C運行時庫負責將命令行分割爲argv
中的單詞。
所以我們需要更多信息:你的shell是什麼,你如何調用你的命令,以及你使用的是哪個C運行時庫?你看到的問題肯定不是Windows本身,我不能用cmd.exe和MSVC的CRT在這裏重現它。
C:>type CON > test.c
#include <stdio.h>
int main(int argc, char **argv) {
int i;
printf("%d\n", argc);
for (i = 0; i < argc; i++)
printf("[%d] <%s>\n", i, argv[i]);
return 0;
}
^Z
C:\>cl test.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
test.c
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:test.exe
test.obj
C:\>test.exe *
2
[0] <test.exe>
[1] <*>
C:\>
如果我使用Cygwin的CRT,並從Cygwin的Bash shell運行,那麼我也沒有看到問題。
C:\>C:\cygwin\bin\bash -l
$ cd /cygdrive/c
$ cc test.c
$ ./a.exe '*'
2
[0] <./a>
[1] <*>
$
這只是如果我嘗試將它們混合—運行使用Cygwin的CRT從CMD的程序。EXE —在那裏我可能看到你的問題
$ exit
C:\>a.exe *
19
[0] <a>
[1] <AUTOEXEC.BAT>
[2] <BOOT.INI>
[3] <CONFIG.SYS>
[4] <Documents and Settings>
[5] <IO.SYS>
[6] <MSDOS.SYS>
[7] <NTDETECT.COM>
[8] <NTLDR>
[9] <Program Files>
[10] <RECYCLER>
[11] <Recycled>
[12] <System Volume Information>
[13] <WINDOWS>
[14] <cygwin>
[15] <hiberfil.sys>
[16] <pagefile.sys>
[17] <temp>
[18] <a.exe>
C:\>
,但我不認爲用雙引號的問題。
C:\>a.exe "*"
2
[0] <test.exe>
[1] <*>
C:\>
雙引號不要爲我工作 - 但我使用的是Windows 7的CMD與海合會與你的測試程序。你使用的是什麼版本的cmd.exe? – 2015-07-29 03:24:01
好吧,我想通了:它是一個mingw32的事情,請參閱https://www.cygwin.com/ml/cygwin/1999-11/msg00052.html – 2015-07-29 03:42:39