开发者

Help with ifstream in programming test

开发者 https://www.devze.com 2023-01-14 04:24 出处:网络
I recently did a programming test in which there was a ifstream section which I could not solve.Since then I have been trying to solve the problem in my free time to no avail.

I recently did a programming test in which there was a ifstream section which I could not solve. Since then I have been trying to solve the problem in my free time to no avail.

The problem is basically to read from a binary file and extract the information there.

Here is the file format:

------------------------------------------------------------------------------
| 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();
}

Thanks.


When you say you are having problems, what output are you seeing, and how is it different from what you expect to see?

Some general advice: For one thing, if you are going to be reading in binary data, try opening the stream in binary mode.

Also, your first read operation stores data into an int, but the length read was unsigned short. Was this intentional?

When you cast your pointer to char* for the first parameter to read, try using an explicit reinterpret_cast<char *> instead.


You're treating numEntities like a 16bit short when (according to the specs and your declaration) it's supposed to be a 32bit integer. You're reading two bytes (sizeof(unsigned short)) instead of 4; try sizeof(int), and throw some of these in there:

assert(sizeof(unsigned short) == 2);
assert(sizeof(int) == 4);
assert(sizeof(float) == 4);
0

精彩评论

暂无评论...
验证码 换一张
取 消