Thread: convert string to const char?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    20

    convert string to const char?

    I am using strings to read in from a text file using getline.

    I am trying to print this string to the screen using the allegro game library calling the function textout_ex.
    textout_ex

    It requires a const char *s ..im using string.. can I convert it?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    if you mean the you are using std::string then yes, std::string::c_str() will give you a character array.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by TenTierHook View Post
    It requires a const char *s ..im using string.. can I convert it?
    Of course you can! Just use the string like this:
    Code:
    yourStr.c_str();
    c_str() returns a pointer to the string's text
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    20
    That worked.

    Thank you. I am starting to realise that C and C++ integrate quite a bit.

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    wow i'm doing allegro too, when I saw the textout_ex() i immediate recognised it !
    we have to read from a file to receive points for playerObjects.
    good luck man

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. strcat - cannot convert char to const char
    By ulillillia in forum C Programming
    Replies: 14
    Last Post: 12-07-2006, 10:00 AM
  3. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 03:46 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM