我最近进行了编程测试,其中有一个ifstream部分无法解决。从那时起,我一直试图在空闲时间解决问题,但无济于事。

问题基本上是从二进制文件读取并在其中提取信息。

这是文件格式:

------------------------------------------------------------------------------
| File Offset (in Bytes)| Value Type                 | Value Description     |
------------------------------------------------------------------------------
| 0                     | Unsigned Integer (32 bits) | number of entities    |
------------------------------------------------------------------------------
| 4                     | Entity information (see    | Entity 0              |
|                       | below)                     |                       |
------------------------------------------------------------------------------
| 4+32                  | Entity Information         | Entity 1              |
------------------------------------------------------------------------------
| ...                   | ...                        | ...                   |
------------------------------------------------------------------------------
| 4 + (32 * N)          | Entity Information         | Entity N              |
------------------------------------------------------------------------------

Entity Information:
------------------------------------------------------------------------------
| Offsett (in Bytes)| Value Type                 | Value Description         |
------------------------------------------------------------------------------
| 0                 | Unsigned short (16 bits)   | Unique ID                 |
------------------------------------------------------------------------------
| 2                 | Unsigned short (16 bits)   | Entity type ID            |
------------------------------------------------------------------------------
| 4                 | Single-precision float (32 | Position X coordinate     |
|                   | bits)                      |                           |
------------------------------------------------------------------------------
| 8                 | Single-precision float (32 | Position Y coordinate     |
|                   | bits)                      |                           |
------------------------------------------------------------------------------
| 12                | Single-precision float (32 | Forward Normal X          |
|                   | bits)                      | Component                 |
------------------------------------------------------------------------------
| 16                | Single-precision float (32 | Forward Normal Y          |
|                   | bits)                      | Component                 |
------------------------------------------------------------------------------

And here is my code:

void readFile()
{
  ifstream ifs ( "binaryFile.bin" , ifstream::in );

  while (ifs.good())
  {
    int numEntities = 0;
    ifs.read( (char*)&(numEntities), sizeof(unsigned short));

    for(int i=0; i<numEntities; i++)
    {
      unsigned short uID;
      unsigned short eID;
      float x;
      float y;
      float forward_x;
      float forward_y;

      ifs.read( (char*)&(uID), sizeof(unsigned short) );
      ifs.read( (char*)&(eID), sizeof(unsigned short) );
      ifs.read( (char*)&(x), sizeof(float) );
      ifs.read( (char*)&(y), sizeof(float) );
      ifs.read( (char*)&(forward_x), sizeof(float) );
      ifs.read( (char*)&(forward_y), sizeof(float) );
    }
  }
  ifs.close();
}

谢谢。

最佳答案

当您说自己有问题时,您会看到什么输出,它与您希望看到的输出有何不同?

一些一般性建议:一方面,如果要读取二进制数据,请尝试以二进制模式open读取流。

同样,您的第一个read操作将数据存储到int中,但是读取的长度为unsigned short。这是故意的吗?

当您将指针转换为char*的第一个参数read时,请尝试使用显式的reinterpret_cast<char *>

10-07 14:04