Passing command line argument with spaces

筅森魡賤 提交于 2019-12-25 06:34:39

问题


I have an application which reads specific commands from the command line and processes the command. For Example:

I have a command called "setparamval" which is used to set the parameter value. The syntax of this command while passing from the command line would look something like this:

setparamval <variable_name>,<value>

Supposing if I have a variable called "DESC" to which I would want to set the value as "ab cd ef gh" I am passing the command line argument as:

setparamval DESC,"ab cd ef gh" 

(i am passing the value inside "", otherwise the command line treats this as seperate arguments..)

Till here everything is fine...

Next, in my code, I am trying to extract the parameter name and the value from the command passed from command line using:

char inputCmd[TAGNAME_LEN];
strcpy(inputCmd,argv[1]);

When i see the value in "inputCmd", i am getting: "DESC,"ab cd ef gh". Now i want to seperate the parameter from the parameter value (where the delimiter is ",") using:

char *savePtr = NULL;
char *src = inputCmd;  //argv[1];
ch = strtok_r(src, ",", &savePtr);

When i see the value inside savePtr, i get: ""ab cd ef gh". There is an extra " getting added to the start of the string. I am not sure why is this getting added.

Could you please help me in figuring out this problem? Thanks for your patience and help.

来源:https://stackoverflow.com/questions/22927779/passing-command-line-argument-with-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!