<tfoot id='eQtg8'></tfoot>

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

    <small id='eQtg8'></small><noframes id='eQtg8'>

      为什么我不能用operator>>读取fstream的二进制数据?

      时间:2024-08-14

        <small id='NJr0L'></small><noframes id='NJr0L'>

          <legend id='NJr0L'><style id='NJr0L'><dir id='NJr0L'><q id='NJr0L'></q></dir></style></legend><tfoot id='NJr0L'></tfoot>

                <tbody id='NJr0L'></tbody>

              <i id='NJr0L'><tr id='NJr0L'><dt id='NJr0L'><q id='NJr0L'><span id='NJr0L'><b id='NJr0L'><form id='NJr0L'><ins id='NJr0L'></ins><ul id='NJr0L'></ul><sub id='NJr0L'></sub></form><legend id='NJr0L'></legend><bdo id='NJr0L'><pre id='NJr0L'><center id='NJr0L'></center></pre></bdo></b><th id='NJr0L'></th></span></q></dt></tr></i><div id='NJr0L'><tfoot id='NJr0L'></tfoot><dl id='NJr0L'><fieldset id='NJr0L'></fieldset></dl></div>
              • <bdo id='NJr0L'></bdo><ul id='NJr0L'></ul>
              • 本文介绍了为什么我不能用operator>>读取fstream的二进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                如果我执行以下操作:

                ifstream file;
                file.open("somefile", ios::binary);
                
                unsigned int data;
                
                file >> data;
                

                我的流将始终设置 failbitdata 将保持未初始化状态.但是,如果我改为读取 charunsigned char ,则流很好.perror() 告诉我结果太大".

                My stream will always set the failbit and the data will remain uninitialized. However, if I read a char or unsigned char instead, the stream is fine. perror() is telling me "result too large".

                我在 Google 上看到的唯一建议是 operator>> 不应该用于二进制数据(首选 read()),但我发现操作符更简洁、更易于使用——而且不需要强制转换所有内容.

                The only thing I saw on Google was a suggestion saying that operator>> shouldn't be used for binary data (prefer read()), but I find the operator to be cleaner and easier to use -- and it doesn't require casting everything.

                有人能解释一下这个问题吗?

                Can someone explain this issue?

                推荐答案

                iostream 提取运算符 (>>) 尝试解释由空格分隔的数字字符串,而不是二进制数据.有许多不同的方法可以对二进制形式的无符号整数进行编码(例如,32 位 2 的补码表示 在 little-endian 字节顺序).这就是为什么您必须使用 read/write 函数对此类二进制缓冲区进行操作.

                The iostream extraction operator (>>) attempts to interpret numerical strings separated by whitespace, not binary data. There are many different ways to encode an unsigned integer in binary form (e.g. a 32-bit 2's complement representation in little-endian byte order). That's why you must use the read/write functions to operate on such binary buffers.

                然而,没有什么可以阻止您使用插入和提取运算符实现自己的类,以您希望的任何形式序列化二进制数据.这样的类可能会在内部使用 ifstream 对象的读取函数.或者,boost 序列化库 可能已经包含正是你想要的.

                However, nothing prevents you from implementing your own class for serializing binary data in whatever form you wish using the insertion and extraction operators. Such a class would likely use the read function of an ifstream object internally. Alternatively, the boost serialization library may already hold exactly what you want.

                这篇关于为什么我不能用operator>>读取fstream的二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:将 std::cout 重定向到自定义编写器 下一篇:缓冲和非缓冲流

                相关文章

                <tfoot id='3YhUH'></tfoot>

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

                  • <bdo id='3YhUH'></bdo><ul id='3YhUH'></ul>
                1. <small id='3YhUH'></small><noframes id='3YhUH'>

                    <legend id='3YhUH'><style id='3YhUH'><dir id='3YhUH'><q id='3YhUH'></q></dir></style></legend>