<i id='C1KS1'><tr id='C1KS1'><dt id='C1KS1'><q id='C1KS1'><span id='C1KS1'><b id='C1KS1'><form id='C1KS1'><ins id='C1KS1'></ins><ul id='C1KS1'></ul><sub id='C1KS1'></sub></form><legend id='C1KS1'></legend><bdo id='C1KS1'><pre id='C1KS1'><center id='C1KS1'></center></pre></bdo></b><th id='C1KS1'></th></span></q></dt></tr></i><div id='C1KS1'><tfoot id='C1KS1'></tfoot><dl id='C1KS1'><fieldset id='C1KS1'></fieldset></dl></div>
    <legend id='C1KS1'><style id='C1KS1'><dir id='C1KS1'><q id='C1KS1'></q></dir></style></legend>
        <bdo id='C1KS1'></bdo><ul id='C1KS1'></ul>

        <tfoot id='C1KS1'></tfoot>
      1. <small id='C1KS1'></small><noframes id='C1KS1'>

        使用 std::ios_base::binary 有什么意义?

        What the point of using std::ios_base::binary?(使用 std::ios_base::binary 有什么意义?)
          <i id='388cA'><tr id='388cA'><dt id='388cA'><q id='388cA'><span id='388cA'><b id='388cA'><form id='388cA'><ins id='388cA'></ins><ul id='388cA'></ul><sub id='388cA'></sub></form><legend id='388cA'></legend><bdo id='388cA'><pre id='388cA'><center id='388cA'></center></pre></bdo></b><th id='388cA'></th></span></q></dt></tr></i><div id='388cA'><tfoot id='388cA'></tfoot><dl id='388cA'><fieldset id='388cA'></fieldset></dl></div>
          • <bdo id='388cA'></bdo><ul id='388cA'></ul>

            <small id='388cA'></small><noframes id='388cA'>

                <tbody id='388cA'></tbody>
                1. <tfoot id='388cA'></tfoot>
                2. <legend id='388cA'><style id='388cA'><dir id='388cA'><q id='388cA'></q></dir></style></legend>
                  本文介绍了使用 std::ios_base::binary 有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 Window 下读取 Linux 文件时遇到问题.以下是问题讨论:使用 fstream::seekg 在 windows 下,在 Unix 下创建的文件上.

                  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.

                  该问题已通过打开指定了 std::ios_base::binarytext 文件解决.

                  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 <<你好世界"<<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 和回车)
                  • 0x0A 作为行分隔符,如果指定了 std::ios_base::binary(仅限 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 或写字板等更好的编辑器确实会显示它们.

                  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?

                  如果我不关心在记事本中打开文件并且想要 fstream::seekg,总是设置 std::ios_base::binary 是否安全总是工作?

                  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?

                  推荐答案

                  二进制和文本模式的区别在于实现定义,但只涉及最低级别:它们不会改变<<>>(插入和提取文本数据).此外,正式地,输出除了一些不可打印的如果文件是文本文件,字符(如 ' ')是未定义的行为模式.

                  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 ' ') is undefined behavior if the file is in text mode.

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

                  For the most common OSs: under Unix, there is no distinction; both are identical. Under Windows, ' ' 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 ' ' 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 有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                  vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                  C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                  Adding types to the std namespace(将类型添加到 std 命名空间)
                  Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                  How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)

                      <tbody id='MjvAw'></tbody>

                    1. <small id='MjvAw'></small><noframes id='MjvAw'>

                        <tfoot id='MjvAw'></tfoot>

                        <legend id='MjvAw'><style id='MjvAw'><dir id='MjvAw'><q id='MjvAw'></q></dir></style></legend>
                      • <i id='MjvAw'><tr id='MjvAw'><dt id='MjvAw'><q id='MjvAw'><span id='MjvAw'><b id='MjvAw'><form id='MjvAw'><ins id='MjvAw'></ins><ul id='MjvAw'></ul><sub id='MjvAw'></sub></form><legend id='MjvAw'></legend><bdo id='MjvAw'><pre id='MjvAw'><center id='MjvAw'></center></pre></bdo></b><th id='MjvAw'></th></span></q></dt></tr></i><div id='MjvAw'><tfoot id='MjvAw'></tfoot><dl id='MjvAw'><fieldset id='MjvAw'></fieldset></dl></div>
                          <bdo id='MjvAw'></bdo><ul id='MjvAw'></ul>