RE: [Dev-C++] execution of a string with system()
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Santiago P. C. <San...@yd...> - 2002-09-18 15:04:38
|
Hi *,
Simon, you can both use a char array (buffer) from the beginning or use
string and then copy the contents to a char array (buffer), anyway this i=
s a
mixed C/C++ situation:
1st)=20
#include <iostream>=20
#include <stdlib.h>
#define BUFFER_SIZE 32
int main()
{=20
char szIP[BUFFER_SIZE];
char szCommand[BUFFER_SIZE];
cout << "Enter the ip to trace:";
cin >> szIP; //enter domain-name of pc
sprintf(szCommand,"tracert %s",szIP);
system(szCommand);
system("PAUSE");
return 0;=20
}=20
Or the 2nd option)
#include <iostream>=20
#include <string>
#include <stdlib.h>
#define BUFFER_SIZE 32
char* ConvertStringToSzData(string strValue)=20
{
=09int iStrLen =3D strValue.size();
=09char* pSzData =3D (char*) malloc( (iStrLen+1)*sizeof(char) );
=09if (pSzData)
=09{
=09 strncpy(pSzData,strValue.data(),iStrLen);
=09 // NULL TERMINATED STRING ;o)
=09 pSzData[iStrLen] =3D '\0';
=09}
=09return pSzData;
}
int main()
{=20
string strIP;
char szCommand[BUFFER_SIZE];
char* pSzIP =3D NULL;
cout << "Enter the ip to trace:";
cin >> strIP; //enter domain-name of pc
pSzIP =3D ConvertStringToSzData(strIP);
if (pSzIP)
{
=09sprintf(szCommand,"tracert %s",pSzIP);
=09free(pSzIP);
=09system(szCommand);
}
else
=09cout << "ERROR: Cannot convert string to char array!" << endl;
system("PAUSE");
return 0;=20
}=20
Hope this helps!
Santy
-----Original Message-----
From: Simon Johner [mailto:el...@op...]=20
Sent: mi=E9rcoles, 18 de septiembre de 2002 16:29
To: dev...@li...
Subject: [Dev-C++] execution of a string with system()
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=3Dstring-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>=20
#include <stdlib.h>
int main()
{=20
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=3D0;i<8;i++){pcno[i+8]=3Dpcno[i];}; //put the domain name at the e=
nd
pcno[0]=3D't'; pcno[1]=3D'r'; pcno[2]=3D'a';
pcno[3]=3D'c'; //enter the dos-command in the f=
irst
chars
pcno[4]=3D'e';
pcno[5]=3D'r';
pcno[6]=3D't';
pcno[7]=3D' ';
for (i=3D16;i<40;i++){pcno[i]=3D' ';};
system(pcno);
system("PAUSE");
return 0;=20
}=20
Does someone know an easier solution?
Tanks for your help.
Simon
--=20
__________________________________________________________
Download the FREE Opera browser at www.opera.com/download/
Free OperaMail at http://www.operamail.com/
Powered by Outblaze
-------------------------------------------------------
This SF.NET email is sponsored by: AMD - Your access to the experts on
Hammer Technology! Open Source & Linux Developers, register now for the A=
MD
Developer Symposium. Code: EX8664 http://www.developwithamd.com/developer=
lab
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
-------------------------------------------------------------------------=
-------------
This message and any files transmitted with it are confidential and inten=
ded solely=20
for the use of the individual or entity to whom they are addressed. No co=
nfidentiality=20
or privilege is waived or lost by any wrong transmission.=20
If you have received this message in error, please immediately destroy it=
=20and kindly=20
notify the sender by reply email.
You must not, directly or indirectly, use, disclose, distribute, print, o=
r copy any=20
part of this message if you are not the intended recipient. Opinions, con=
clusions and=20
other information in this message that do not relate to the official busi=
ness of=20
Ydilo Advanced Voice Solutions, S.A. shall be understood as neither given=
=20nor endorsed by it.=20
-------------------------------------------------------------------------=
-------------
|