how to prevent losing double quotes in argv?

后端 未结 2 1809
抹茶落季
抹茶落季 2021-01-21 10:15

Ok, I know I can do loop like this:

for (int i=1; i<=argc-1;i++) {
  cout << argv[i] << endl;
}

But this way double quotes are l

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-21 11:02

    It's the shell that removes the quotes. If you want the shell to pass the quotes to you, you have to escape them:

    $ ./my-program \"hello\ world\"
    

    Note that I escape the space as well, to make it one argument.

提交回复
热议问题