Re: [Dev-C++] execution of a string with system()
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Abhijit S. <mu...@gm...> - 2002-09-18 16:41:49
|
Greets.
> Hello everybody,
>
> I tried to write a programm which does execute some dos command (like
tracert, ping etc.) with an ip.
>
> At first, I created a string (#include <string>), but when I tried to
execute it with the system()-command, I got the following error
(exec=string-Variable):
> "cannot convert 'exec' from type string to type 'const char*'"
>
> Finally -after some hours of trying :)- I've got the following solution:
>
> #include <iostream>
> #include <stdlib.h>
>
>
> int main()
> {
> int i;
> char pcno[20];
>
> cout << "Enter the ip to trace:" , cin >> pcno; //enter domain-name of pc
(domain-name is supposed to be 8 chars long)
>
> for (i=0;i<8;i++){pcno[i+8]=pcno[i];}; //put the domain name at the end
> pcno[0]='t';
> pcno[1]='r';
> pcno[2]='a';
> pcno[3]='c'; //enter the dos-command in the
first chars
> pcno[4]='e';
> pcno[5]='r';
> pcno[6]='t';
> pcno[7]=' ';
> for (i=16;i<40;i++){pcno[i]=' ';};
>
> system(pcno);
> system("PAUSE");
> return 0;
> }
>
>
> Does someone know an easier solution?
Try this (untested and unparsed, minor changes maybe necessary :)
string whatever = "foo";
system(foo.c_str());
___________________________________________________________
Abhijit Shylanath
E-mail: mu...@gm... || ibr...@bi...
Web-site: http://mudeth.tripod.com/
|