Command line argument to string in C -


is there way in c store whole command line options , arguments in single string. mean if command line ./a.out -n 67 89 78 -i 9 string str should able print whole command line. now, able print values in different vector forms.

#include <stdio.h> #include <getopt.h> #include <string.h>   int main(int argc, char* argv[]) {  int opt;  for(i=0;i<argc;i++){ printf("whole argv %s\n", argv[i]); }  while((opt = getopt(argc, argv, "n:i")) != -1) { switch (opt){     case 'n':              printf("i %s\n", optarg);              break;      case 'i':              printf("i %s\n", optarg);              break;       }    }   return 0;  } 

i want this, optarg printing first argument , want arguments printed, want parse after storing in string.

simply write function this:

char * combineargv(int argc, char * * argv) {     int totalsize = 0;     (int = 0; < argc; i++)     {        totalsize += strlen(argv[i]);     }     // provides space ' ' after each argument , '\0' terminator.     char *ret = malloc(totalsize + argc + 1);     if (null == ret)     {         // memory allocation error.     }     (int = 0; < argc; i++)     {         strcat(ret, argv[i]);         strcat(ret, " ");     }     return ret; } 

this combine of them, placing spaces between args

update: have modified original eliminate buffer overflow issue.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -