我想使用以下代码从文件中读取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的目的将任何对象解释为字符数组。)