从文件中读取uint32_t值

Read uint32_t values from file

本文关键字:uint32 读取 文件      更新时间:2023-10-16

我想使用下面的代码从文件中读取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的目的)