Why does ifstreams 'open' take a const char* and not a string type?

This is a discussion on Why does ifstreams 'open' take a const char* and not a string type? within the C++ Programming forums, part of the General Programming Boards category; So ifstream is part of the C++ string containers/classes and afaik , char is a C type, so why does ...

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    86

    Why does ifstreams 'open' take a const char* and not a string type?

    So ifstream is part of the C++ string containers/classes and afaik, char is a C type, so why does is it ,

    Code:
    ifstream fileToOpen
    fileToOpen.open(const char*);
    and not,

    Code:
    ifstream fileToOpen
    fileToOpen.open(string);

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by jim_0 View Post
    So ifstream is part of the C++ string containers/classes and afaik, char is a C type, so why does is it ,

    Code:
    ifstream fileToOpen
    fileToOpen.open(const char*);
    and not,

    Code:
    ifstream fileToOpen
    fileToOpen.open(string);
    The more basic type is char*, which can be used with std::string like this:
    Code:
    std::string s = "myfile";
    std::ifstream ifs;
    ifs.open(s.c_str());
    If it was only possible with a std::string then you couldn't use a plain old C-style string.

    However, in C++11 the function is overloaded to allow a std::string or a C-style string.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    C++11 adds an overload that takes a std::string. you're likely using an older compiler, or you're not enabling the features of C++11. if you're using GCC/G++ 4.4 or above, you can add the compiler switch -std=c++0x or -std=c++11 (depending on the version of the compiler), and you should be able to use std::string for this. I'm not sure about other compilers, as I have little or no experience with them.
    Code:
    namespace life
    {
        const bool change = true;
    }

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    86
    Quote Originally Posted by Elkvis View Post
    C++11 adds an overload that takes a std::string. you're likely using an older compiler, or you're not enabling the features of C++11. if you're using GCC/G++ 4.4 or above, you can add the compiler switch -std=c++0x or -std=c++11 (depending on the version of the compiler), and you should be able to use std::string for this. I'm not sure about other compilers, as I have little or no experience with them.

    Ah ok, thanks!

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,340
    Quote Originally Posted by jim_0 View Post
    So ifstream is part of the C++ string containers/classes and afaik, char is a C type, so why ...
    Could be historical, so a character pointer from argv[] from main() could be used?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    Could be historical,
    If I remember correctly it had more to do with timing. I believe the std::string class, as we know it today, was a late addition to the standard process.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String and const char *
    By sawer in forum C++ Programming
    Replies: 5
    Last Post: 03-05-2006, 02:16 AM
  2. const char[] or string?
    By prog-bman in forum C++ Programming
    Replies: 10
    Last Post: 07-11-2004, 07:13 PM
  3. Converting type string to type const char*
    By rusty0412 in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 06:59 PM
  4. Converting from type char to const char*
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2003, 10:51 PM
  5. oprantds errors of type int and const char [31] to binary???
    By Gaidin0147 in forum C++ Programming
    Replies: 1
    Last Post: 04-09-2003, 08:53 PM

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