我想使用下面的代码从文件中读取 uint32_t 整数。 ifstream 只接受指向 char 数组的指针。有没有另一种方法可以使用类似于下面的代码读取 uint32_t 值?
int readCount;
uint32_t buffer[SIZE];
while ( fin.read( &buffer[0], SIZE)
|| (readCount = fin.gcount()) != 0 ) {
// some code
}
最佳答案
使用强制转换,例如:
if (fin.read(reinterpret_cast<char *>(buffer), sizeof buffer) &&
fin.gcount() == sizeof buffer)
{
// use buf
}
(明确允许将任何对象解释为字符数组,正是为了 I/O 的目的。)
关于c++ - 从文件中读取 uint32_t 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23418818/