Archived

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

Radix3d

working with cmd.exe

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
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
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