fstream(string)

This is a discussion on fstream(string) within the C++ Programming forums, part of the General Programming Boards category; Why this should not work? Code: #include <iostream> #include <fstream> #include <string> int main() { using namespace std; string str ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    fstream(string)

    Why this should not work?
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main()
    {
        using namespace std;
        string str = "C:\\test.del";
        ifstream f(str);
    }
    Why fstream doesn't accept a string as a path?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    fstreams take a const char* as the filename, so you have to call c_str() on the string to pass it to the fstream constructor or open function.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    fstreams take a const char* as the filename, so you have to call c_str() on the string to pass it to the fstream constructor or open function.
    I know that, but it is wrong.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You mean that you think they should change it? They might change it in the next standard. In the previous standard, fstreams were already widely used before the string class was created.

    I agree that there should be an overload for strings, but I don't think its that big of a deal.

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    The much bigger problem with fstream is the lack of any way to provide a wchar_t pathname.

    That means WinNT systems can't express pathnames in the native character set of their machine, and thus it's impossible in standard C++ to open all possible filenames (you need to use WinAPI file handling in lieu of C++ streams).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    You mean that you think they should change it? They might change it in the next standard.
    Of course they should change it. Because at least standard library should support itself. It is now contrary to what Stroustrup has in mind.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

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