用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藏汉因为只有看到2个参数,如果我有&LT; 并在命令行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?
感谢
推荐答案
有是命令行和标准输入之间的差异 - 15是一个命令行参数。外壳看到'&LT;'和重定向的文件,你的程序在它的标准输入
流。
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.
如果你不希望使用标准输入来处理文件,只需通过它的名字,并打开自己: ./一致性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屋!