C 中的命令行参数打印不正确 [英] Command Line argument in C not printing correctly
问题描述
我正在尝试测试我的程序如何接收用户命令行输入:
im trying to test how my program is receiving a users command line input:
我要测试的命令行输入是:
my command line input to test is:
"./concordance 15 < input.txt"
程序的其余部分工作,但测试参数.所以在我的主要功能中,我有这个:
the rest of the program works but to test the arguments. so in my main function i have this:
int main(int argc, char *argv[])
{
int i;
for (i = 0; i < argc; i++)
{
printf("%s\n", argv[i]); //runs through command line for arg
}
printf("%d\n", argc); //prints total arguments
return 0;
}
问题是当我输入命令行时,程序打印:
The problem is when I enter my command line, the program prints:
./concordance
15
2
为了让我的程序工作,我需要打开 input.txt 文件,所以我的问题是,为什么程序只打印./concordance"和15",并且如果我有<"和命令行中的input.txt"?
for my program to work I need to open the input.txt file so my question is, why is the program only printing "./concordance", and "15" aswell as only seeing 2 arguments if I have "<" and "input.txt" in the command line?
谢谢
推荐答案
命令行和stdin"之间有一个区别——15"是一个命令行参数.外壳看到<"并将文件重定向"到您的程序中的 stdin
流.
There is a difference between the command line and "stdin" - "15" is a command line argument. The shell sees the '<' and "redirects" the file to your program on it's stdin
stream.
如果您不想使用 stdin 来处理文件,只需传递它的名称并自己打开:./concordance 15 input.txt
(argv[2] 将是 input.txt)
If you don't want to use stdin to process the file, just pass it's name and open yourself: ./concordance 15 input.txt
(argv[2] will be input.txt)
这篇关于C 中的命令行参数打印不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!