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

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

    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
    3,445
    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.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    107
    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,658
    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
    4,633
    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, 06:13 PM
  3. Converting type string to type const char*
    By rusty0412 in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 05:59 PM
  4. Converting from type char to const char*
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2003, 09: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, 07:53 PM