StringName

This is a discussion on StringName within the C++ Programming forums, part of the General Programming Boards category; How is it possible to change "TheFile.txt" to a string instead. From this: Code: Ifstream Onefile ("TheFile.txt"); I am trying ...

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

    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
    Portugal
    Posts
    7,514
    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*
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    383
    Okay. I understand. This worked great. I will look at this.

    Thanks...

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,823
    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

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