使用std :: ios_base :: binary有什么意义? [英] What the point of using std::ios_base::binary?

查看:751
本文介绍了使用std :: ios_base :: binary有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Window下读取Linux文件时遇到问题。这是问题的讨论:使用fstream :: seekg在Unix下创建的文件的Windows下。

I had a issue with Linux file reading under Window. Here is the issue discussion: Using fstream::seekg under windows on a file created under Unix.

通过使用<$打开 text 文件可以解决此问题。 c $ c> std :: ios_base :: binary 指定。

The issue was workarounded by opening the text file with std::ios_base::binary specified.

但是此模式的实际意义是什么?如果已指定,您仍可以将文件作为文本文件使用(使用 mystream<< Hello World<< std :: endl 编写并使用 std :: getline )。

But what's the actual point with this mode? If specified, you can still work with your file as a text file (writting with mystream << "Hello World" << std::endl and reading with std::getline).

在Windows下,唯一的区别是,我注意到的是 mystream<< Hello World<< std :: endl 使用:

Under Windows, the only difference, I could notice is that mystream << "Hello World" << std::endl uses:


  • 0x0D 0x0A 如果未指定 std :: ios_base :: binary (EOL和回车)作为行分隔符

  • 如果指定了 std :: ios_base :: binary (仅EOL),则将0x0A 作为行分隔符(仅EOL)

  • 0x0D 0x0A as line separator if std::ios_base::binary was not specified (EOL and carriage return)
  • 0x0A as line separator if std::ios_base::binary was specified (EOL only)

打开由 std :: ios_base :: binary 生成的文件时,记事本无法巧妙地显示行。像vi或Wordpad这样的更好的编辑器确实可以显示它们。

Notepad does not smartly show lines when opening the files generated with std::ios_base::binary. Better editors like vi or Wordpad does show them.

实际上,使用和不使用 std :: ios_base生成的文件之间唯一的区别是: :binary ?文档说将流视为二进制而不是文本。,这到底意味着什么?

Is that really the only difference there is between files generated with and without std::ios_base::binary? Documentation says Consider stream as binary rather than text., what does this mean in the end?

安全吗?总是设置 std :: ios_base :: binary 如果我不在乎在记事本中操作文件,并且想拥有 fstream :: seekg 总是可以工作吗?

Is it safe to always set std::ios_base::binary if I don't care about opeing the file in Notepad and want to have fstream::seekg always work?

推荐答案

二进制和文本模式之间的差异是实现
的定义,但是只关注最低级别:它们不会更改>> 含义c $ c>(可插入和提取文本
数据)。同样,如果文件的文本为$ b $,则输出除少数几个不可打印的
字符(如'\n')以外的所有字符也是未定义的行为

The differences between binary and text modes are implementation defined, but only concern the lowest level: they do not change the meaning of things like << and >> (which insert and extract textual data). Also, formally, outputting all but a few non-printable characters (like '\n') is undefined behavior if the file is in text mode.

对于最常见的操作系统:在Unix下,没有区别。两者都是相同的。在Windows下,内部'\n'将在外部映射到两个
字符序列CR,LF(0x0D,0x0A),而0x1A将是
在读取时解释为文件结尾。但是,在更具异国情调(且绝大部分为
的操作系统)中,它们可能在操作系统级别由完全不同的
文件类型表示,并且可能无法以
文本模式读取文件如果是以二进制模式编写的,反之亦然。否则,您
可能会看到不同的东西:行尾有多余的空格,或者
在二进制模式下没有'\n'

For the most common OSs: under Unix, there is no distinction; both are identical. Under Windows, '\n' internally will be mapped to the two character sequence CR, LF (0x0D, 0x0A) externally, and 0x1A will be interpreted as an end of file when reading. In more exotic (and mostly extinct) OSs, however, they could be represented by entirely different file types at the OS level, and it could be impossible to read a file in text mode if it were written in binary mode, and vice versa. Or you could see something different: extra white space at the end of line, or no '\n' in binary mode.

关于始终设置 std :: ios_base :: binary :我对
便携式文件的政策是确定确切的格式,设置
二进制,然后输出所需的格式。通常是CR,LF,而不仅仅是
LF,因为这是网络标准。另一方面,大多数
Windows程序仅使用LF就没有问题,但是我遇到的
比一些使用CR,LF的Unix程序要多。
主张系统地仅使用LF(这也更容易)。这样操作
意味着无论
是在Unix还是Windows下运行,我都能得到相同的结果。

With regards to always setting std::ios_base::binary: my policy for portable files is to decide exactly how I want them formatted, set binary, and output what I want. Which is often CR, LF, rather than just LF, since that's the network standard. On the other hand, most Windows programs have no problems with just LF, but I've encountered more than a few Unix programs which have problems with CR, LF; which argues for systematically using just LF (which is easier, too). Doing things this way means that I get the same results regardless of whether I'm running under Unix or under Windows.

这篇关于使用std :: ios_base :: binary有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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