Thread: 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
-------------------------------------------------------------------------=
-------------
|
|
From: Stringer, A. <Adr...@as...> - 2002-09-18 15:19:26
|
--- #include "stdio.h" #include "string.h" #include "stdlib.h" --- I may be wrong here, and if so feel free to slap me, but I think that ought to be #include <cstdio> #include <cstring> #include <cstdlib> and your own header files should be included as #include "myheader.h" See the end of http://www.comeaucomputing.com/techtalk/#cname. I quote: --> Similarly, the Standard C headers, such as <stdio.h>, are supported in C++, but they are deprecated. Instead, so-called Cname headers are preferred, for instance, cstdio, or cctype instead of ctype.h Furthermore, names in the .h are assumed to be in the global namespace, and so therefore do not need to be qualified with std::. The .h form is sometimes used as a transition model for backwards compatibility. Or, for a "co-ed" source file able to be compiled by a C or C++ compiler. <-- Sorry to be pedantic, Adrian |
|
From: Christophe C. <chr...@fr...> - 2002-09-19 07:32:52
|
Hello,
think Abhijit Shylanath mistyped his response - should be (untested too) :
string whatever = "foo";
system(whatever.c_str());
This will execute the command "foo" on your system.
Little tip : system() returns an integer value, but on many systems, this
is not the plain return code of the executed command. If you need to test
the command's return code, use (will work on many - but maybe not all -
systems) :
int returncode=system(whatever.c_str()) >> 8;
This is because on these systems, the return code of the command is stored
in the 8 upper bits of a 16 bits integer. The ">> 8" says "take all the
bits of this integer and shift them 8 times on right". Thus, the 8 upper
bits takes place of the 8 lower ones, and the value of the whole integer
becames now the command's return code.
Example :
0011001100011100 >> 8 = 0000000000110011
Before ending, a little warning : if you want to execute system commands in
a multi-threaded program, you MUSN'T use the system() function. You must
then use a combination of fork() and for example waitpid() commands.
If you need a sample, query on the list.
|
|
From: Bob W. <bo...@af...> - 2002-09-19 07:52:44
|
Can anyone give an example of this? Christophe Chailloleau wrote: >--clip-- > >Before ending, a little warning : if you want to execute system commands in >a multi-threaded program, you MUSN'T use the system() function. You must >then use a combination of fork() and for example waitpid() commands. >If you need a sample, query on the list. > >--clip== > -- -- Bob ' Sally:"So a man can be friends with a woman he finds unattractive?" Harry:"No, you pretty much want to nail them, too" ' ICQ: 77149742 AOL: Sanchrin MSN: Bo...@af... http://www.aftermath.cx/ |
|
From: Abhijit S. <mu...@gm...> - 2002-09-19 16:27:13
|
> > Hello, > > think Abhijit Shylanath mistyped his response - should be (untested too) : > Yes, he did. Dumb guy. Serves him right for mixing up ambiguity. ... > Before ending, a little warning : if you want to execute system commands in > a multi-threaded program, you MUSN'T use the system() function. You must > then use a combination of fork() and for example waitpid() commands. > If you need a sample, query on the list. This is, unfortunately, platform-dependant, and on Windows, you should use CreateProcess(). Same last sentence. ___________________________________________________________ Abhijit Shylanath E-mail: mu...@gm... || ibr...@bi... Web-site: http://mudeth.tripod.com/ |