2010-09-03 664 views
2

我最近做了一个编程测试,其中有一个我无法解决的ifstream部分。从那以后,我一直试图在空闲时间解决这个问题而无济于事。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     | 
------------------------------------------------------------------------------ 

这里是我的代码:

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

感谢。

+0

为什么不让'numEntities'成为'unsigned short'? – meagar 2010-09-03 21:01:15

回答

2

当你说你有问题时,你看到了什么输出,它与你期望看到的有什么不同?

一些一般性建议:首先,如果您打算使用二进制数据读取数据,请尝试使用二进制模式下的数据流。

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

当您将指针指向char*作为read的第一个参数时,请尝试使用明确的reinterpret_cast<char *>来代替。

+1

我只是为了让邻居听到它而努力。谢谢。 – dornad 2010-09-03 21:04:25

+0

如果你想确保你正在读取每个项目的正确的字节数,使用略少出错的语法'sizeof(numEntities)'而不是'sizeof(int)'。 – bta 2010-09-03 21:12:10

2

你正在对待numEntities像一个16位短(当根据规格和您的声明)它应该是一个32位整数。您正在读取两个字节(sizeof(unsigned short))而不是4个;尝试sizeof(int),并将其中的一些内容扔在那里:

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