为什么 STL ifstream 和 ofstream 类不采用 std::string 作为文件名?

发布于 2024-08-30 04:47:08 字数 744 浏览 3 评论 0原文

这是对STL的抱怨。为什么他们将文件名参数作为 (char *) 而不是 std::string ?这似乎没有道理。

关于此主题还有两个问题:

问题是我有很多看起来像这样的代码:

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

WhenI would like it to look like this:

std::ofstream f(fname);

上述帖子中提到的其他问题是 UTF-16 与 UTF-8 的问题。 (UTF-16 可能包含 NULL,这会破坏 POSIX API)。但这并不是真正的问题,因为该实现可以在调用 open() 之前将 UTF-16 转换为 UTF-8。

但说真的,这没有任何意义。有升级STL的计划吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鼻尖触碰 2024-09-06 04:47:08

为什么 ifstream 和 ofstream 类不采用 std::string 作为文件名?

我已经看到了一些合理的论点(即这会创建流对字符串的依赖),但坦率地说,我相信真正的原因是流比标准库及其字符串要旧得多。

有升级STL的计划吗?

它被称为 C++11,将成为该标准的新版本。我不知道文件流是否改变。您可以查看最终草案 并亲自找出答案。

请注意,STL 是容器、算法和迭代器库的名称,已合并到标准库中。标准库的一部分还有字符串、流等。
特别是,流不是 STL 的一部分。他们是兄弟姐妹。

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.

一腔孤↑勇 2024-09-06 04:47:08

历史原因。 iostream 库是与字符串内容分开开发的。但为什么它没有集成到 C++ 标准中,大家都在猜测。我很久以前就在 Usenet 上看过几个问题(包括依赖理论),但从来没有一个真正令人满意的解释。

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.

宛菡 2024-09-06 04:47:08

我记得,实际上(至少在某种程度上)是 string 与 wstring 的情况。我现在找不到该帖子,但我相当确定我记得 Andrew Koenig 的 Usenet 帖子,说它是由一个国家委员会的成员提出的(我似乎记得日本,但可以很容易出错)提出了他们如何处理各种语言的文件名的问题(特别是因为当时相对较少的操作系统对此提供了很多支持)。尽管一开始非常简单,但很快就发现,避免它变得巨大混乱的唯一方法就是停止对这个想法的所有讨论。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文