Converting an std::string to a const char *

This is a discussion on Converting an std::string to a const char * within the C++ Programming forums, part of the General Programming Boards category; Well, I'd like to keep it that way if I could. But the ifsteam inputfile function chokes if I try ...

  1. #16
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Well, I'd like to keep it that way if I could.

    But the ifsteam inputfile function chokes if I try to give it an std::string
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #17
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    that's the "as long as you can" bit

    use std::string like this

    Code:
    bool MS3DModel::Load_MS3D_Model(const std::string &Filename)
    {
    	ifstream inputFile( Filename.c_str(), ios::in | ios::binary | ios::nocreate );
    // more code here!
    }
    that should work.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #18
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Thats what I had in there about 10 posts ago
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    With the assert you mentioned:
    Code:
    bool MS3DModel::Load_MS3D_Model(const std::string &Filename)
    {
    	assert(Filename.length() > 5 && Filename.substr(Filename.length() - 5) == ".ms3d");
    	ifstream inputFile( Filename.c_str(), ios::in | ios::binary | ios::nocreate );
    // more code here!
    }

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Shamino
    Thats what I had in there about 10 posts ago
    Except you were creating a temporary.

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    And passing by value.

  7. #22
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Yeah yeah , checkout the full system WIP, I posted about it..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcat - cannot convert char to const char
    By ulillillia in forum C Programming
    Replies: 14
    Last Post: 12-07-2006, 09:00 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 02:46 AM
  4. Display Lists (OpenGL)
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 06-05-2005, 12:11 PM
  5. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21