• Advertisement

Archived

This topic is now archived and is closed to further replies.

working with cmd.exe

This topic is 5071 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to write a simple C++ program that will: 1) open cmd.exe 2) issue a simple DOS command to cmd.exe through my program (myprogram.exe.) 3) execute the command (basically like pressing the enter key after you type the command) Could someone explain how to do this, or even better, provide the code to do this? Thanks

Share this post


Link to post
Share on other sites
Advertisement
The system() function does this for you.


Thanks Salsa!Colin Jeanne | Invader''s Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud

Share this post


Link to post
Share on other sites
Alright I figured out how to use the system() function (thanks btw), but I ran into a problem. When I try to use:

system( C3 );

where C3 is a global char variable holding a DOS command, I get the error message:

error C2664: ''system'' : cannot convert parameter 1 from ''const char'' to ''const char *''

Share this post


Link to post
Share on other sites
errrm.... how are you defining C3?


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
#include <conio.h>
#include <process.h>
#include <iostream.h>

char C1;

void Module1()
{
cout << "Enter Command ===>" << endl;
cin >> C1;
cout << endl;
}

void main()
{

Module1();

system( C1 );

getch();
}

Share this post


Link to post
Share on other sites
Also, as written (provided C1 were an array), that would only get the input up to the first space, which probably isn''t what you want. Look into cin.getline or similar.

Share this post


Link to post
Share on other sites
Alright I used an array and got it to work (thanks guys) but I still don''t understand why it didn''t accept a regular char variable? Is it like a type safe feature or something?

Share this post


Link to post
Share on other sites
How many DOS commands do you know of that are one character long?


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
I''m just using CIN for testing. I understand its limitations and what other funtions I can use.

Share this post


Link to post
Share on other sites
Try:


int main(int argc, char* argv[])
{
if(argc != 1)
std::cout << "huh? i don''t understand your arguments." << std::endl;
else
for(;;)
{
std::cout << "> ";
std::string cmd;
std::getline(std::cin, cmd);
if(cmd == "exit")
break;
std::system(cmd.c_str());
}
}


- Pete

Share this post


Link to post
Share on other sites

  • Advertisement