9

This is a complaint about STL. Why do they take filename arguments as (char *) and not as std::string? This seems to make no sense.

There are two other questions on this topic:

The issue is that I have a lot of code that looks like this:

    std::ofstream f(fname.c_str());

WhenI would like it to look like this:

std::ofstream f(fname);

Additional issues that are mentioned in the above posts is the issue of UTF-16 vs. UTF-8. (UTF-16 might contain NULLs which would break the POSIX API). But that's not really an issue, because the implementation could convert UTF-16 to UTF-8 before calling open().

But seriously, this makes no sense. Are there any plans to upgrade STL?

Community
  • 1
  • 1
vy32
  • 28,461
  • 37
  • 122
  • 246
  • Is the issue `char*` vs `std::string` or `char*` vs `wchar_t*`? So long as the OS is consistent about multibyte behavior, a `char*` should suffice. The question is whether `fstream::fstream` is supposed to like results from `wcstombs` (and, simply, safe conversion of `wstring` to `string` at all). – Potatoswatter Apr 26 '10 at 21:35
  • This does suck because suddenly programmers who have been told about all the wonders of OO and of how they don't need to know how objects are implemented have to go back and learn some details of how things are put together. – nategoose Apr 26 '10 at 21:43
  • 3
    @nategoose: if by "learn some details of how things are put together", you mean, "call `c_str()`", then yes ;-) You don't really need to know what a C-style string actually is for this purpose, just that filename parameters need one, and that there's a way to get one. IMO a worse issue is that string + string concatenates, string + literal concatenates, but literal + literal mysteriously crashes... – Steve Jessop Apr 26 '10 at 22:02
  • I edited the question to have a code example. I agree that literal+literal is stupid. In Objective-C there is a shortcut for having NSStrings--- @"foo" gives you a "foo" string. Nothing like that for C++, though another language created for people who love to type... – vy32 Apr 27 '10 at 06:44
  • possible duplicate of [Why don't the std::fstream classes take a std::string?](http://stackoverflow.com/questions/32332/why-dont-the-stdfstream-classes-take-a-stdstring) – maxschlepzig Mar 13 '14 at 18:07

3 Answers3

10

why don’t ifstream and ofstream classes take std::string as filenames?

I've seen a few sensible arguments for that (namely that this would create a dependency of the streams on strings), but frankly I believe the actual reason is that the streams are much older than the standard library and its strings.

Are there any plans to upgrade STL?

It's called C++11 and will be the new version of the standard. I don't know whether file streams changed. You could look at the final draft and find out for yourself.

Note that STL is the name for a library of containers, algorithms, and iterators, incorporated into the standard library. Also part of the standard library are strings, streams and others.
In particular, streams are not part of the STL. They are siblings.

sbi
  • 219,715
  • 46
  • 258
  • 445
  • 6
    A quick look at the draft shows that `string` will allowed as a filename but `wstring` will not. – Potatoswatter Apr 26 '10 at 21:30
  • @Potatoswatter: surely you mean "`string` will be mandatory, but `wstring` is an extension". C++98 allowed extra overloads, and I don't recall a change there? – MSalters Apr 27 '10 at 09:56
  • @MSalters: Well, GNU in any case does not have such an extension, and Mac OS X in any case doesn't provide a syscall `wopen` accepting `wchar_t*`. So it looks like UTF-8 is the portable way to go. – Potatoswatter Apr 27 '10 at 17:27
3

Historical reasons. The iostream library was developed separately from the string stuff. But why this wasn't integrated in the C++ Standard is anyone's guess. I've seem several questions on Usenet way back when (including the dependancy theory) , but never a really satisfactory explanation.

2

As I recall, it actually is (at least sort of) the situation with string vs. wstring. I can't find the post right now, but I'm reasonably certain I remember a Usenet post by Andrew Koenig saying that it had been brought up by members of one of the national committees (Japan is what I seem to recall, but could easily be wrong) brought up the question of how they could deal with file names in various languages (especially since relatively few OSes at the time provided much support for that). Even though it had started out pretty simple, it quickly became apparent that the only way to avoid it turning into a huge mess was to cease all discussion of the idea in general.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111