【发布时间】:2022-10-12 23:34:31
【问题描述】:
所以我有这个代码:
#include <stdio.h>
int main(int argc, char **argv) {
//Reassign input arguments into local values
// Check if inputs are valid
// translate the input string
//assign the list into a nested string
//search for translated string in the list
//save all found cases
//print all found cases
int i = 0;
for (i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
printf("%d",argc);
return 0;
}
输入后:outDebug.exe 你好 <seznam.txt进入命令提示符...
它给了我这些回报:
argv[0] = outDebug.exe
argv[1] = 你好
2
如果文件不在,文件在哪里argv?
【问题讨论】:
-
它通过
stdin或文件描述符0引用。 -
<seznam.txt不是 main 的 arg,而是从该文件重定向stdin。 -
在这个以这种方式调用的程序中,您可以从
stdin中读取,并且您将从该文件中获得输入。就像您明确说出FILE *ifp = fopen("seznam.txt", "r");或FILE *ifp = fopen(argv[1], "r");,然后从ifp读取一样。 -
类似的问题:尝试调用
outDebug.exe "hello world",然后问,引号去哪儿了? -
重定向的全部意义在于程序不必做任何事情。它只是正常地从标准输入中读取。如果输入被重定向,它会从文件中读取。如果不是,它从终端读取。
标签: c cmd arguments program-entry-point