我有以下问题。我需要从文件 xxx.obj 中读取整数。整数始终为 4B。
如果我使用 hexdump -C 来归档,它看起来是:
04 00 00 00 06 00 00 00 08 00 00 00 50 00 00 00
一对是 1B。
这是我打开文件的方式:
ifstream fs1;
fs1.open( srcFile1, ios::in | ios::binary );
这就是我如何在循环中从文件中读取 1B:
while( fs1.get( c ) )
{
cnt = (int)(unsigned char)c;
cout << cnt << endl;
}
输出是:
4
0
0
0
6
0
0
0
8
0
0
0
80
0
0
0
知道如何直接读取 int 或如何读取 4x 1B 然后转换为 int 吗?
最佳答案
我建议使用 int32_t
数据类型,这样您就可以确保获得一个 4 字节的 int。或者,如果您需要无符号数,请使用 uint32-t
。
int32_t x; // Make sure integer is 4 bytes
fs1.read(&x, sizeof(x)); // Read from file
关于c++ - 从 C++ 中的 .obj 文件中读取整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36227698/