从文件中读取uint32_t值

时间:2014-05-02 00:15:11

标签: c++ file-io input io

我想使用下面的代码从文件中读取uint32_t整数。 ifstream只接受指向char数组的指针。有没有其他方法可以使用类似下面的代码读取uint32_t值?

int readCount;
uint32_t buffer[SIZE];
while ( fin.read( &buffer[0], SIZE)
        || (readCount = fin.gcount()) != 0 ) {
    // some code
}

1 个答案:

答案 0 :(得分:1)

使用演员,例如:

if (fin.read(reinterpret_cast<char *>(buffer), sizeof buffer) &&
    fin.gcount() == sizeof buffer)
{
    // use buf
}

(明确允许将任何对象解释为字符数组,正是为了I / O的目的。)