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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run a system command from C++ 1

Status
Not open for further replies.

MCSGraham

Programmer
May 28, 2002
52
US
I'm asking a user what filename they're wanting to work with then wanting C++ to issue a system command like it was being run from the command prompt.

This should be simple enough but can't seem to get it right. Here's some code I have:

string FileName;

cout<<"What is the file you want to work with?: "<<endl<<endl;
cin>>FileName;

system("ren FileName test.txt");


Doing this tells me the file is not specified but the file is there so I don't know why it won't work.

Not sure if I'm doing the 'system' command correctly?

Does anyone have any suggestions for this?

thanks in advance!
 
Well if you're using strings, it would be something like

Code:
cout<<"What is the file you want to work with?: "<<endl<<endl;
cin>>FileName;
string command = "ren " + Filename + " test.txt";
system( command.c_str() );

--
 
ren FileName test.txt" mean the same as you see between quotes. So if value of FileName will be xxx.txt the string will remain "ren FileName test.txt". In C++ you can not use variables in the same way as in PHP or PERL.
You should build a string like this pseudocode:
command = "ren " + FileName + " test.txt";
system(command);

Ion Filipski
1c.bmp
 
Thanks a lot for your help! Your suggestion worked!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top