简体   繁体   English

命令行参数计数

[英]Command Line Argument Counting

This is a simple C program that prints the number of command line argument passed to it: 这是一个简单的C程序,它打印传递给它的命令行参数的数量:

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("%d\n", argc);
}

When I give the input 当我给出输入

file_name *

It prints 623 instead of 2 in my pc (operating system Windows 7). 它在我的电脑(操作系统Windows 7)中打印623而不是2。 But it gives the correct output in other cases. 但它在其他情况下提供了正确的输出。 Is * a reserved character for command line arguments? *是命令行参数的保留字符吗? Note this program gives correct output for the following input: 请注意,此程序为以下输入提供正确的输出:

file_name *Rafi

Output = 2 输出= 2

On a Unix command line, the shell is responsible for handling wildcards. 在Unix命令行上,shell负责处理通配符。 yourapp * will run yourapp , and pass the name of ALL of the non-hidden files in the current directory as arguments. yourapp *将运行yourapp ,并将当前目录中所有非隐藏文件的名称作为参数传递。 In your case, that's 622 files (623 = 622 files + name of the program). 在你的情况下,这是622个文件(623 = 622个文件+程序的名称)。

On Windows, applications are responsible for wildcard parsing, so argc is 2, 1 for the name of the program (argv[0]) and 1 for the wildcard (argv[1] = *); 在Windows上,应用程序负责通配符解析,因此对于程序名称(argv [0]),argc为2,1,对于通配符,argc为1(argv [1] = *);

*由shell或运行时库(前者在* nixes上,后者在Windowses上)扩展,而不是文字*您将获得当前工作目录中所有文件的名称。

As others have mentioned, you're getting the 'shell wildcard expansion' or 'globbing' where the * is used as a wildcard to match file names to place in the argv array. 正如其他人所提到的,你正在获得'shell通配符扩展'或'globbing',其中*用作通配符来匹配文件名以放置在argv数组中。

On Unix systems this is performed by the shell and has nothing (or little) to do with the C runtime. 在Unix系统上,这是由shell执行的,并且没有(或很少)与C运行时有关。

On Windows systems, this functionality is not performed by the shell (unless possibly if you're using some Unix-like shell replacement like Cygwin). 在Windows系统上,shell不执行此功能(除非您使用像Cygwin这样的类似Unix的shell替换)。 The globbing functionality may or may not be performed by the C runtime's initialization depending on what tools and/or linker options you use: 根据您使用的工具和/或链接器选项,C运行时的初始化可能会也可能不会执行通配功能:

  • if you're using Microsoft's compiler, the C runtime will not perform globbing by default, and you would get an argc value of 2 in your example. 如果您正在使用Microsoft的编译器,则C运行时默认情况下不会执行通配,并且您的示例中的argc值将为2。 However, if you ask the linker to link in setargv.obj (or wsetargv.obj if you have a Unicode build), then globbing is added to the runtime initialization and you'll get behavior similar to Unix's. 但是,如果您要求链接器链接到setargv.obj (如果您有Unicode构建, wsetargv.obj ),然后将globbing添加到运行时初始化中,您将获得类似于Unix的行为。 setargv.obj has been distributed with MSVC for as long as I can remember, but it's still little known. setargv.obj已经与MSVC一起分发,只要我记得,但它仍然鲜为人知。 I believe that most Windows programs perform their own wildcard expansion. 我相信大多数Windows程序都会执行自己的通配符扩展。

  • if you're using the MinGW/GCC tool chain, the C runtime will perform globbing before calling main() (at least it does for MinGW 4.6.1 - I suspect it's been in MinGW for a long time). 如果您正在使用MinGW / GCC工具链,C运行时将在调用main()之前执行通配(至少它适用于MinGW 4.6.1 - 我怀疑它已经在MinGW中使用了很长时间)。 I think MinGW might not perform globbing for GUI programs. 我认为MinGW可能不会为GUI程序执行globbing。 You can disable MinGW's globbing behavior with one of the following: 您可以使用以下方法之一禁用MinGW的通配行为:

    1. define a global variable named _CRT_glob and initialize it to 0 : 定义一个名为_CRT_glob的全局变量并将_CRT_glob初始化为0

       int _CRT_glob = 0; 
    2. link in the lib/CRT_noglob.o object file (I think this might be order dependent - you may need to place it before any libraries): lib/CRT_noglob.o目标文件中的链接(我认为这可能依赖于顺序 - 您可能需要将它放在任何库之前):

       gcc c:/mingw/lib/CRT_noglob.o main.o -o main.exe 

The problem is that the shell expands * into all the file names (that don't start with a . ) in the current directory. 问题是shell将*扩展为当前目录中的所有文件名(不以.开头)。 This is all about the shell and very little to do with the C program. 这完全是关于shell的,与C程序关系不大。

The value of argc includes 1 for the program's own name, plus one for each argument passed by the shell. argc的值包括1为程序自己的名称,加上一个由shell传递的每个参数。

Try: 尝试:

filename *
filename '*'

The first will give you 623 (give or take - but it is time you cleaned up that directory!). 第一个将给你623(给予或采取 - 但现在是你清理该目录的时候了!)。 The second will give you 2. 第二个会给你2。

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

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