Re: Passing Variables To System()
From: John Harrison (john_andronicus_at_hotmail.com)
Date: 06/30/04
- Next message: John Harrison: "Re: order of const in function prototypes"
- Previous message: Niels Dybdahl: "Re: defining or not defining destructors"
- In reply to: JLK: "Re: Passing Variables To System()"
- Next in thread: JLK: "Re: Passing Variables To System()"
- Reply: JLK: "Re: Passing Variables To System()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 30 Jun 2004 09:39:15 +0100
"JLK" <jonkokko@gmail.com> wrote in message
news:f9001473.0406292355.3cb4f638@posting.google.com...
> Leor Zolman <leor@bdsoft.com> wrote in message
news:<ut34e0le99nuj7cg072tcdnk0etrn2md33@4ax.com>...
> > On 29 Jun 2004 17:49:59 -0700, jonkokko@gmail.com (JLK) wrote:
> >
> > >I'm having a bit of a time with the following code. I can script this
> > >real easy in Bash but I'm trying to practice my C++:
> > >
> > >*******************************************************
> > >#include <iostream>
> > >#include <string>
> > >using namespace std;
> > >int main () {
> > > string user = "fred";
> > > string cmd;
> > > cmd = "curl -d userid=" + user + "&press=submit http://URL";
> > > cout << "command = " << cmd << endl;
> > > cout << "command = " << cmd.c_str() << endl;
> > > system(cmd.c_str());
> > >return 0;
> > >}
> > >*******************************************************
> > >
> > >If you run it you'll notice that both of the couts will produce the
> > >full line ok. However, if I try to pass either one to system() it will
> > >fail as nothing past "curl -d userid=" gets appended. How can you
> > >pass variables such as parameters to system() ??
> >
> > Depending upon what operating system you're running under (and it sounds
> > like yours is some flavor of Unix), the '&' character is going to have
> > different meanings to the command processor/shell. I happen to be
testing
> > under Windows XP and running 4NT as my command processor; the '&' is an
> > "end of command" separator just like ';' is under the Unix shells. On
your
> > machine, wouldn't '&' mean "run the previous command in the background"
?
> > And the remainder would be interpreted as setting an environment
variable
> > named 'press' to the value 'submit', with the 'http' being then seen as
the
> > start of yet another command?
> >
> > I'd reocommend stragetic placement of some backslashes, for starters...
> > -leor
>
>
>
> You are correct as I'm running linux and the & symbol is a
> 'background' command. However, that is only true if the character
> stands alone at the end of a command (such as: script.sh &) If it gets
> correctly appended and runs as "curl -d userid=user&press=submit
> http://URL" then it would be ok. It appears my variable insert is not
> getting appended along with the other half of the script. The weird
> part is that if I hard code USER I can run everything OK. I'm only
> bombing out when I try to insert it as a variable.
No, something else is going on. Your cmd string is correct, as proved by the
previous to cout << statements. By the time your program gets to the system
call, it is completely irrelevant whether the cmd string was composed of
variables or not, its just a string.
What you are saying is that you get different results with two identical
strings depending on whether that string was original composed from a
variable or not. Frankly, that is impossible, something else is going wrong.
john
- Next message: John Harrison: "Re: order of const in function prototypes"
- Previous message: Niels Dybdahl: "Re: defining or not defining destructors"
- In reply to: JLK: "Re: Passing Variables To System()"
- Next in thread: JLK: "Re: Passing Variables To System()"
- Reply: JLK: "Re: Passing Variables To System()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|