Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variables in system() call C++

I have a variable in argv[1] which I need to call inside another c++ file in such a way:

        system("./assemblerL.out", argv[1]);

this above is incorrect syntax as I am getting "too many arguments" errors.

What is the correct way of doing this?

like image 949
Barney Chambers Avatar asked Feb 10 '23 22:02

Barney Chambers


1 Answers

system can take only one parameter, and that´s a whole line like entered in the shell
(well, not with everything a shell like Bash got, but that doesn´t matter here).

With C++, just use std::string to concat the parts:

system((std::string("./assemblerL.out ") + argv[1]).c_str());  

Or, more readable:

std::string line = "./assemblerL.out ";
line += argv[1];
system(line.c_str());  

Remember to make sure that argv[1] exists before using it.

like image 109
deviantfan Avatar answered Feb 13 '23 03:02

deviantfan