C语言 如何从C中的命令行参数读取“字符串”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5046035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read "string" from command line argument in C?
提问by mike
I've a question about passing in parameters via the command line.
我有一个关于通过命令行传递参数的问题。
My main()looks like
我的main()样子
int main(int argc, char **argv){
int b, d, n, flag;
char *init_d, tst_dir[100];
argv++;
init_d=*(argv++);
//printf(); <--------What do I have to do to init_d so that I can print it later?
If argvis a pointer to an array of pointers I'm assigning init_dto point to the value being pointed to by the pointer argvpoints to? (If that even makes sense)
如果argv是一个指向我分配的指针数组的指针init_d,指向该指针所指向的值argv?(如果这甚至有意义)
I assume I have to get that value into a character array in order to print it out but if I don't know the size of the "string" I am passing in, I am not sure how to achieve this. For instance if i run my code './myprogram hello' compared to './myprogram alongerinput'
我假设我必须将该值放入一个字符数组中才能打印出来,但是如果我不知道我传入的“字符串”的大小,我不知道如何实现这一点。例如,如果我运行我的代码 './myprogram hello' 与 './myprogram alongerinput' 相比
回答by David Heffernan
You can print the arguments without transferring them into character arrays. They are null-terminated C strings and printfeats them for breakfast:
您可以打印参数而不将它们传输到字符数组中。它们是空终止的 C 字符串,printf早餐吃它们:
for (i=0; i<argc; i++)
printf("%s\n", argv[i]);
回答by Marlon
argvis a pointer to an array of null-terminated strings. You do not need to know the size of the string; all you need to do is point to the value since the end of the string is indicated with a '\0'.
argv是指向以空字符结尾的字符串数组的指针。您不需要知道字符串的大小;您需要做的就是指向该值,因为字符串的末尾用 '\0' 表示。
char* init_d = argv[0];
printf("%s", init_d);
If you did want to know the length of the string you could use strlen(argv[0]).
如果您确实想知道字符串的长度,可以使用strlen(argv[0]).
回答by Ari Malinen
Here is example that converts command line arguments to single string:
这是将命令行参数转换为单个字符串的示例:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
if (argc < 1)
return 0;
int i;
int strsize = 0;
for (i=1; i<argc; i++) {
strsize += strlen(argv[i]);
if (argc > i+1)
strsize++;
}
printf("strsize: %d\n", strsize);
char *cmdstring;
cmdstring = malloc(strsize);
cmdstring[0] = 'int main(int argc, char **argv){
while(--argc>0)
printf("%s\n",*++argv);
return 0;
}
argc: argument count i.e. number of arguments in command line
argv: argument vector
';
for (i=1; i<argc; i++) {
strcat(cmdstring, argv[i]);
if (argc > i+1)
strcat(cmdstring, " ");
}
printf("cmdstring: %s\n", cmdstring);
return 0;
}
回答by Cratylus
init_d = *(argv++);
if your program is myProgthen myProg helloin command line has argc=2 (including program name), argv[0] is "myProg", and argv[1] is "hello"
如果程序是myProg然后myProg hello在命令行已的argc = 2(包括节目名称),argv的[0]是“MYPROG”,和argv [1]是“你好”
回答by charleyc
After running
运行后
printf("%s", init_d);
(parens not necessary, btw), init_d is a pointer to a character. In C, a string is a character pointer that adheres to the contract that if it is advanced far enough, eventually a null character ('\0') will be reached, signifying the end of the string. In other words, init_d is now precisely the string you want. You can print it with
(不需要括号,顺便说一句), init_d 是一个指向字符的指针。在 C 中,字符串是一个字符指针,它遵守约定,如果它前进到足够远,最终将到达一个空字符 ('\0'),表示字符串的结尾。换句话说,init_d 现在正是您想要的字符串。你可以用它打印
printf("%s", init_d);
Note, btw, that *argv++ is giving you the first element of argv, which is actually the name of the function on the command line. You probably want the first command line argument, which you'll get with *++argv.
注意,顺便说一句,*argv++ 给你 argv 的第一个元素,它实际上是命令行上函数的名称。您可能需要第一个命令行参数,您可以使用 *++argv 获得它。
回答by Fox32
You can output the string you pointing with init_d at with
你可以输出你用 init_d 指向的字符串
##代码##"%s" output all characters of the string until it reach a '\0'. In C, strings are null terminated.
"%s" 输出字符串的所有字符,直到它到达 '\0'。在 C 中,字符串以空字符结尾。

