PPS
PPS

Reputation: 53

CMD.exe quoting a string

What's the exact way to properly quote a single command line argument? For example, I have some random text in a variable $X. I need to quote it in a way so that if I call

system("program.exe " + $X_QUOTED);

then argv[1] of my program.exe has to match original unquoted $X

imagine I have this program.exe:

int main(const char **argv, int){ puts(argv[1]); }

and the output of command: "program xxxx" is:

"test |test

what xxxx has to literally be? I tried to add quotes and all that trickery, but then I can always add some other type of output that would break my approach to quote cmd line arguments.

Upvotes: 2

Views: 492

Answers (1)

Joey
Joey

Reputation: 354714

H:>args """test |test"
argv[0] = args
argv[1] = "test |test

Apparently:

  • Replace each quote by ""
  • Surround the argument with quotes

Upvotes: 2

Related Questions