Thread: StringName

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    StringName

    How is it possible to change "TheFile.txt" to a string instead.
    From this:
    Code:
    Ifstream Onefile ("TheFile.txt");
    I am trying to instead declare "TheFile.txt" like this but this doesn´t work. What could be wrong here.

    Code:
    std::string File;
    File = "TheFile.txt"
    
    Ifstream Onefile (File);

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    std::string File( "TheFile.txt" );
    std::ifstream Onefile( File.c_str() );

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    that constructor expects a const char*. the cpompiler cannot convert from std::string to const char*.

    ifstream OneFile (file.c_str()) is what you need. the c_str() member function of the std::string class returns your string as a const char*
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Okay. I understand. This worked great. I will look at this.

    Thanks...

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Mario F. View Post
    that constructor expects a const char*. the cpompiler cannot convert from std::string to const char*.
    That's because the poor thing lacks an overloded operator const char* as opposed to a real string class such as CString
    I know I'm asking for trouble
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed