string " removed from argv input

D

devlanguage

Hi,

The string " which is part of the argv input is removed.

// test input : ./a.out dev "query"
// actual output : query output
// expected output : "query" output

#include <iostream>

int main(int argc, char* argv[])
{
while (argc > 0)
{
std::cout << argv[argc-1] << std::endl;
--argc;
}
return 0;
}

Does anybody know what is the reason for this ?

thanks
Dev.
 
V

Victor Bazarov

The string " which is part of the argv input is removed.

// test input : ./a.out dev "query"
// actual output : query output
// expected output : "query" output

#include <iostream>

int main(int argc, char* argv[])
{
while (argc > 0)
{
std::cout << argv[argc-1] << std::endl;
--argc;
}
return 0;
}

Does anybody know what is the reason for this ?

That's your shell playing tricks with you. Escape the quotes with \

V
 
A

Alf P. Steinbach

* (e-mail address removed):
The string " which is part of the argv input is removed.

// test input : ./a.out dev "query"
// actual output : query output
// expected output : "query" output

#include <iostream>

int main(int argc, char* argv[])
{
while (argc > 0)
{
std::cout << argv[argc-1] << std::endl;
--argc;
}
return 0;
}

Does anybody know what is the reason for this ?

The command line arguments can be processed at at least two levels:

1) the command interpreter, and

2) the C++ runtime library (before passing them to main).

Your example command indicates a Unix-like environment.

In that case it's only the command interpreter you have to take into
account. A typical Unix command interpreter removes quotes because it
supports quoting, and it supports quoting so that you can e.g. include a
space character in an argument, and the support typically includes features
you can use to have the command interpreter regard quote symbols as ordinary
characters (check your documentation). This is relevant for C++ because the
C++ 'main' is designed with just such an environment in mind, and e.g. the
Windows standard command interpreter is not such an environment, which means
that C++ is heavily Unix-oriented, not platform-agnostic -- unfortunately.

PS: Why don't you switch the order of the two statements in the 'while'
loop? That let's you eliminate one arithmetic operation while retaining
readbility. You can also, if you want, and if you have a standard
compiler, remove the 'return 0' command, which is the default for 'main'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top