在二进制模式下打开输入文件流时设置skipws标志 [英] skipws flag set when opening an input file stream in binary mode

查看:309
本文介绍了在二进制模式下打开输入文件流时设置skipws标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道抽取运算符不应该用于以二进制模式打开的输入流,但应该使用成员函数 read

I know the extraction operator should not be used on an input stream opened in binary mode, but the member function read should be used instead.

std::ifstream ifs("file.bin", std::ios::in | std::ios::binary);
char c;
ifs >> c; // Should not be used
ifs.read(&c, 1); // OK

但它可以做。所以我的问题是什么是不打开在二进制模式打开输入文件流skipws标志的理由?

But it can be done anyway. So my question is what is the rationale for not unsetting the skipws flag on input file streams when opened in binary mode?

推荐答案

二进制模式,由 std :: ios_base :: binary 控制仅用于关闭标准C ++ \\\
字符和系统特定的换行序列存储在文件中。

"Binary" mode, as controlled by std::ios_base::binary is only for switching off the translation of newlines between the standard C++ \n character and the system specific newline sequence as stored in files.

它完全独立于您是否解析包含有意义的分隔空格的文件完全不同的字节格式,因此没有理由将两个正交的功能绑定在一起。

It's completely independent of whether you are parsing a file that contains meaningful separating whitespace or some completely different byte format so there's no reason to tie the two orthogonal pieces of functionality together.

(C ++标准没有说明什么二进制模式意味着,在C标准中更详细地讨论了文本流和二进制流之间的潜在差异。二进制流必须读取字节,因为它们被写在任何给定的系统上,而文本流只需要这样做,给定一些限制,在换行符之前有多余的空格,并且除了换行符和制表符之外没有任何控制字符。系统不需要在二进制流和文本流之间做任何区分。)

(The C++ standard doesn't say much about what binary mode means, there is more detail in the C standard which talks about the potential differences between text streams and binary streams. Binary streams must read back byte for byte as they were written on any given system whereas text stream need only do so given a number of restrictions centring around not having extra spaces before a newline and not having any control characters other than newlines and tabs. A system need not make any distinction at all between binary and text streams.)

这篇关于在二进制模式下打开输入文件流时设置skipws标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆