-2

How would I pass a command line variable though a system() command in c++. I have tried using:

string i;
i = system("./findName.sh");
i += argv[1];
cout << i;

But when i run this it gives me my condition for wrong number of arguments i have written in my shell script.

This is the output I received when running my program with "./findName brandonw". Which is my executable file ran with the argument i want my shell script to run with.

The arguments you put are:
brandonw
usage: findName.sh [only_one_argument]

2 Answers2

1

Just concatenate it to the command string.

string command = "./findName.sh";
command = command + " "  + argv[1];
system(command.c_str());
Joey Allen
  • 381
  • 1
  • 4
  • 12
0

Just rearrange your code a bit:

string i("./findName.sh ");
i += argv[1];
system(i.c_str());
cout << i;

Also note, that system doesn't return a std::string, but an implementation defined int value.

If you need to deal with ./findName.sh's output, you rather need pipe().

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190