以二进制模式打开 ifstream 是否需要 ios::in ?

发布于 2024-12-04 23:45:56 字数 221 浏览 0 评论 0原文

这两者有什么区别? in 标志对象不是多余的吗?谢谢。

std::ifstream file1("one.bin", std::ifstream::in | std::ifstream::binary);

std::ifstream file2("two.bin" ,std::ifstream::binary);

What's the difference between these two? Isn't the in flag object thing redundant? Thanks.

std::ifstream file1("one.bin", std::ifstream::in | std::ifstream::binary);

std::ifstream file2("two.bin", std::ifstream::binary);

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

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

发布评论

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

评论(4

戏剧牡丹亭 2024-12-11 23:45:56

来自 ifstream 类构造函数的文档:

二进制(二进制)将流视为二进制而不是文本。
in(输入)允许在流上进行输入操作。

因此,当从文件中读取时,我会使用 std::ifstream::in 标志,不是因为它是必需的(或不是),而是因为让编程接口知道您要执行的操作是一种很好的编程实践。将使用它。

编辑:
以下内容摘自http://www.cplusplus.com/doc/tutorial/files/< /a>,关于 open() 成员函数(但是问题中代码中的构造函数可能调用 open() 复制模式标志而不进行修改)。

类: 默认模式参数
ofstream: ios::out
ifstream: ios::in
fstream:ios::in | ios::输出

对于 ifstream 和 ofstream 类, ios::in 和 ios::out 是
自动并分别假设,即使模式不
包含它们作为第二个参数传递给 open() 成员
功能。

尽管如此,网络上的许多示例在显示 ifstream 对象的构造时都使用 ifstream::in。可能真的是某种迷信行为,而不是编程行为。

From the docs on ifstream class constructor:

binary (binary) Consider stream as binary rather than text.
in (input) Allow input operations on the stream.

So when reading from a file, I would use std::ifstream::in flag not because it's required (or not) but because it would be a good programming practice to let a programming interface know what you are going to use it for.

Edit:
The following is taken from http://www.cplusplus.com/doc/tutorial/files/, about open() member function though (but the constructors in the code in the question probably call open() copying the mode flags without modification).

class: default mode parameter
ofstream: ios::out
ifstream: ios::in
fstream: ios::in | ios::out

For ifstream and ofstream classes, ios::in and ios::out are
automatically and respectively assumed, even if a mode that does not
include them is passed as second argument to the open() member
function.

Nevertheless, many examples over the Web use ifstream::in when showing a construction of an ifstream object. Could really be some kind of a superstition practice, instead of a programming one.

ゃ懵逼小萝莉 2024-12-11 23:45:56

binary,在这种情况下,仅指读取或写入的方法。在 Windows 上的常规模式下,'\n' 被转换为 '\r''\n'。这会影响读取和写入,因此二进制模式会将其关闭。 out|binaryin|binary 一样有意义

binary, in this case, only refers to the method of reading or writing. In regular mode on windows, '\n' is translated to '\r''\n'. This can affect both reading and writing, so binary mode turns this off. out|binary makes just as much sense as in|binary

我爱人 2024-12-11 23:45:56

我在网上找不到权威文档。

编辑我什至无法在我的副本Josuttis Book,第 8 次印刷。 它应该在第 13.9 节第 627-631 页

经验证据表明它是多余的,IFF std::ios::in 或 std::ios:out 都没有通过:

#include <fstream>
#include <iostream>

int main(int argc, char** args)
{
    std::ifstream ifs(args[0], std::ios::binary);
    std::cout << ifs.rdbuf() << std::flush;

    return 0;
}

成功:

test | md5sum
md5sum test

显示相同哈希总和。


    // ...
    std::ifstream ifs(args[0], std::ios::out | std::ios::binary);

将失败(零字节输出)

test | wc -c  # shows 0

I can't find authoritative documentation online.

Edit I can't even find a proper reference in my copy the Josuttis Book, 8th printing. It should have been in section 13.9 pp. 627-631

Empirical evidence suggests it is redundant IFF none of std::ios::in or std::ios:out are passed:

#include <fstream>
#include <iostream>

int main(int argc, char** args)
{
    std::ifstream ifs(args[0], std::ios::binary);
    std::cout << ifs.rdbuf() << std::flush;

    return 0;
}

Succeeds:

test | md5sum
md5sum test

show the same hash sum.


    // ...
    std::ifstream ifs(args[0], std::ios::out | std::ios::binary);

will fail (zero bytes output)

test | wc -c  # shows 0
羁拥 2024-12-11 23:45:56

cplusplus.com 参考页面来看,没有区别。

in 始终为 ifstream 对象设置(即使在参数 mode 中明确未设置)。< /p>

ofstream 也是如此。因此,您不需要为 ifstream 设置 std::ios::in 或为 std::ios::out 设置>ofstream,即使您设置了 std::ios::binary 并忽略了 in/out 模式。

From cplusplus.com reference page, there is no difference.

in is always set for ifstream objects (even if explicitly not set in argument mode).

It's the same for ofstream. Therefore, you don't need to set std::ios::in for ifstream or std::ios::out for ofstream, even if you have set std::ios::binary which omits the in/out mode.

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