Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Variables to system() 1

Status
Not open for further replies.

FinnMan

Technical User
Feb 20, 2001
75
US
I'm trying to do the following with much unsuccess:

************************
#include <iostream>
#include <cstdlib>
using namespace std;
string user;
main()
{
cin >> user;
system("curl -d userid="+user+"&press=submit
}
***********************

I've tried every combo of variable type I can think but no luck. Where am I going wrong?


Thx,
FM
 
You need to build a command line

Code:
string cmd = "curl -d userid=" + user + "&press=submit [URL unfurl="true"]https://url";[/URL]
system( cmd.c_str() );  // get the 'C' string from the string

--
 
Well that definately compiles better! I'm not sure I understand the syntax though as I haven't seen it before with other system commands. Is that because of the variable input?

Also, it now no longer recognizes anything past "curl -d userid=" Am I appending everything ok?

Best Regards,
FM
 
Well system() performs actions pretty much like you would type them at a command prompt, so normally its something like
Code:
system( "ls -l *.c" );

In C, you would create a dynamic command like so
Code:
char cmd[100];
sprintf( cmd, "ls -l %s", input );
system( cmd );

You can play with the strings in whatever form you want, but the one you end up passing to system() must be a 'C' style string.

> Am I appending everything ok?
No idea.
I was assuming from your first post that whatever string class you are using supports string concatenation by overloading the + operator.
Code:
cout << cmd;
to make sure it looks OK

--
 
Ok, I'm stumped. If I pass the string to cout then the display of the command is ok. If I pass it to system then it fails (although it does compile ok). It appears to not like the way I'm attaching the variable user into the string. That seems odd to me because I would think that it would also fail with cout if it was incorrect.
 
What were you expecting curl to do?

Does curl do the right thing if you copy/paste the command you think you are executing to a command line prompt?

Most 'useful' curl commands (IMO) have the '--output' command line option to dump whatever is received to a file.
It could be just working and throwing the results away.



--
 
Yes, the overall syntax of the curl command is correct. I'd post the url for you but it's an internal company page with no public access.

Ultimately what I'm trying to do is write a C++ script that will read from a list of users and run the curl command for each user, then I'll parse the data and pull what I need.

When I run the compiled script it cuts off at "curl -d userid=", without appending the rest of the data and thus the curl command (not the script)fails. Would it make a significant difference if I put system() as a separate function?

I can do this quite successfully in a shell script but I'm trying to teach myself more C++. Thx for your pointers, it's very appreciated!


FM.
 
> Would it make a significant difference if I put system() as a separate function?
No, the problem appears to be that your string class does not support +

Code:
#include <iostream>
#include <string>
using namespace std;
int main ( ) {
    string user = "fred";
    string cmd;
    cmd = "curl -d userid=" + user + "&press=submit [URL unfurl="true"]https://url";[/URL]
    cout << "command = " << cmd << endl;
    cout << "command = " << cmd.c_str() << endl;
    return 0;
}

Gives
[tt]command = curl -d userid=fred&press=submit command = curl -d userid=fred&press=submit [/tt]
It's the 2nd form which you need to be calling system() with.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top