variables in system() function

Hi
I haven't touched C/C++ for over 15 years and even then it was "hello world"
What I would like to do and have started to do is have a C++ code to run an executable with options after the command

Here is the current code(which compiles OK and works)
1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <stdlib.h>

int main(void)
{

system("psftp.exe IP address -l testuser -pw testpw -b upload.scr");
return 0;
}

I would like to replace the "IP address", "testuser" and "testpw" with variables declared in the code somewhere. Can someone assist on how I would do this?
I don't need to take commands from the command line just run a command

Thanks
John
Last edited on
1
2
3
std::string line = "psftp.exe " + IP + " -l " + testuser  + " -pw " + testpw + " -b upload.scr";
// IP, testuser and testpw is an std::string variables too
system(line.c_str());
Please use code tags in future posts (or even your post above, by editing it).
http://cplusplus.com/articles/jEywvCM9/

I would like to replace the "IP address", "testuser" and "testpw" with variables declared in the code somewhere. Can someone assist on how I would do this?
I don't need to take commands from the command line just run a command

You basically need to construct a custom string.
One of the easiest ways to construct it is by using string streams.

As opposed to MiiNiPaa's method, you can easily insert non-string data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::stringstream ss;

    std::string name("UserName");
    int age = 30;
    float height = 9.87f;

    ss << "echo Name is: " << name << ", age is " << age << ", height is " << height << '.';
    system(ss.str().c_str());
}
Thanks for the replies.
I think I am with you, I will have a dabble and see what I can achieve.
Oh and I have included code tags :-)
@ jpe33: your original code looks like C code, and not C++.

If you happen to use an old C compiler, you will be unable to compile my and MiiNiPaa's example code.

You may want to:
1) read about C++: http://cplusplus.com/doc/tutorial/
2) download a modern C++ compiler: http://nuwen.net/mingw.html
@catfish, Ahhh ....mmmm

I am using Quincy 2005 which compiled the code below fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::stringstream ss;

    std::string name("UserName");
    int age = 30;
    float height = 9.87f;

    ss << "echo Name is: " << name << ", age is " << age << ", height is " << height << '.';
    system(ss.str().c_str());
}


Ok. Seeing though I am here, can I do want I want to do with CPP?

Quincy 2005 looks a bit dated (the 2008 version uses MinGW 4.2 according to its site) but if it works for you then I guess there's no reason to use something else.

Ok. Seeing though I am here, can I do want I want to do with CPP?

What do you mean? Your Quincy 2005's C++ compiler should support C++03 well enough, for now it should be enough.

C++ has so far three standards:

1) C++98 (the original from 1998)

2) C++03 (from 2003, which is different from C++98 only under the hood, but they're basically the same on the outside)

3) C++11 (the long awaited new standard of 2011, used to be called C++0x, has many additions and improvements)

You're fine with C++03 for now. The World didn't fully move on to C++11 just yet.
Topic archived. No new replies allowed.