Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

How do I convert string variable to char*??

Question posted by: kaushiknanda (Newbie) on July 3rd, 2008 02:02 AM
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
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
kaushiknanda's Avatar
kaushiknanda
Newbie
3 Posts
July 3rd, 2008
02:05 AM
#2

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*)'

Reply
scruggsy's Avatar
scruggsy
Member
123 Posts
July 3rd, 2008
03:34 AM
#3

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.:
Code: ( text )
  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.

Reply
kaushiknanda's Avatar
kaushiknanda
Newbie
3 Posts
July 3rd, 2008
09:31 PM
#4

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.:
Code: ( text )
  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
Reply
Not the answer you were looking for? Post your question . . .
182,533 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top C / C++ Forum Contributors