问题
Say I have a program like this:
int main(int argc, char **argv){
printf("The name of the program is %s, and the string passed is:\n %s \n\n", argv[0], argv[1]);
return 0;
}
I realize there should be error checking and whatnot, but what I'd really like to know is how to pass a whole string as the first argument or if it's even possible. Thanks!
回答1:
Just use quotes
./file_name "This is the long string argument that comes in argv[1]"
|
|
+--- argv[0]
Edit:
aruisdante: single and double quotes are treated depending on the shell, double quotes might do unexpected things
PS: My answer comes from Windows platform, where use of single quote, simply skips everything after whitespace
来源:https://stackoverflow.com/questions/23458204/can-i-pass-a-whole-string-as-one-command-line-argument-into-argv-in-c