gpt4 book ai didi

c++ - 在编程测试中帮助ifstream

转载 作者:搜寻专家 更新时间:2023-10-31 01:21:40 24 4
gpt4 key购买 nike

我最近做了一个编程测试,其中有一个我无法解决的 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 *>相反。

关于c++ - 在编程测试中帮助ifstream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3639518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com