Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can i use string in function system();?

Normally I do system("something");
How can I do something like that:

string c;
cin>>c;
system(c);
Posted

See string::c_str[^].
If this is a wise application is another question - looks like a security-hole. You execute here any command with the access rights of the program running system().
Regards
Andi
 
Share this answer
 
 
Share this answer
 
It is just the same, a string variable or string constant produce exactly the same results. However, you need to add some code to check exactly what is contained in the string before you pass it to system, to protect from hacking. Something like:
C++
string strCommand;
cin >> strCommand;
// code to validate the command, and ensure
// it is not used maliciously. E.g. "format c:"
//
system(strCommand.c_str());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900