Connecting Tech Pros Worldwide Help | Site Map

How do I convert string variable to char*??

Newbie
 
Join Date: Jul 2008
Posts: 3
#1: Jul 3 '08
hey,
I am pretty new to c++ programming and I am supposed to use system(const char*). But I have a string variable as the argument. Can any1 please help me how to use this string variable as argument to system()? thanks in advance.
-kaushik
Newbie
 
Join Date: Jul 2008
Posts: 3
#2: Jul 3 '08

re: How do I convert string variable to char*??


Quote:

Originally Posted by kaushiknanda

hey,
I am pretty new to c++ programming and I am supposed to use system(const char*). But I have a string variable as the argument. Can any1 please help me how to use this string variable as argument to system()? thanks in advance.
-kaushik


i m getting the following error:

cannot convert 'std::string' to 'constant char*' for argument '1' to 'int system(constant char*)'
Familiar Sight
 
Join Date: Mar 2007
Posts: 148
#3: Jul 3 '08

re: How do I convert string variable to char*??


Quote:

Originally Posted by kaushiknanda

i m getting the following error:

cannot convert 'std::string' to 'constant char*' for argument '1' to 'int system(constant char*)'

std::string has a c_str() function that returns a const char*, i.e.:
Expand|Select|Wrap|Line Numbers
  1. std::string stringThing("A C++ string");
  2. const char* cStyleString = stringThing.c_str();
Quick google-search or peek at the index of a C++ reference book should get you acquainted with other useful std::string methods.
Newbie
 
Join Date: Jul 2008
Posts: 3
#4: Jul 3 '08

re: How do I convert string variable to char*??


Quote:

Originally Posted by scruggsy

std::string has a c_str() function that returns a const char*, i.e.:

Expand|Select|Wrap|Line Numbers
  1. std::string stringThing("A C++ string");
  2. const char* cStyleString = stringThing.c_str();
Quick google-search or peek at the index of a C++ reference book should get you acquainted with other useful std::string methods.

thanks a lot ...it worked
Reply