English 中文(简体)
如何从C中的命令行参数中读取“字符串”?
原标题:How to read "string" from command line argument in C?
  • 时间:2011-02-18 20:10:49
  •  标签:
  • c

我有一个关于通过命令行传递参数的问题。

我的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?

如果argv是指向指针数组的指针,我会将init_d分配给指针所指向的值?(如果这有道理的话)

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

最佳回答

您可以打印参数,而无需将它们转换为字符数组。它们是以null结尾的C字符串,printf早餐吃它们:

for (i=0; i<argc; i++)
  printf("%s
", argv[i]);  
问题回答

以下是将命令行参数转换为单个字符串的示例:

#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
", strsize);

    char *cmdstring;
    cmdstring = malloc(strsize);
    cmdstring[0] =   ;

    for (i=1; i<argc; i++) {
        strcat(cmdstring, argv[i]);
        if (argc > i+1)
            strcat(cmdstring, " ");
    }

    printf("cmdstring: %s
", cmdstring);

    return 0;
}

argv is a pointer to a range of un-terminated strings. 你们不需要知道护卫的大小;你们需要做的是,自护卫的结束以来,你的价值是用表示的。

char* init_d = argv[0];
printf("%s", init_d);

如果您确实想知道字符串的长度,可以使用strlen(argv[0])

int main(int argc, char **argv){ 

    while(--argc>0)
       printf("%s
",*++argv);
    return 0;
}

argc: argument count i.e. number of arguments in command line
argv: argument vector 

如果您的程序是myProg,那么命令行中的myProg-hello具有argc=2(包括程序名),argv[0]是“myProg”,argv[1]是“hello”

运行后

init_d = *(argv++);

in(不需要, b)是个性点。 在C,扼杀是遵守合同的一个特点,即如果合同进展得很远,最终将达到无效性质(),这标志着扼杀的结束。 换言之,现在的用心正是你想要的。 您可以将其印成文。

printf("%s", init_d);

顺便说一句,请注意,*argv++为您提供了argv的第一个元素,它实际上是命令行上函数的名称。您可能想要第一个命令行参数,它将通过*++argv获得。

您可以使用输出用init_d指向的字符串

printf("%s", init_d);

“%s”输出所有星体特性,直至达到。 在C,扼杀物无效。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签