从文件读取uint32_t值 [英] Read uint32_t values from file

查看:672
本文介绍了从文件读取uint32_t值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用下面的代码从文件中读取uint32_t整数。 ifstream只接受一个指向char数组的指针。有没有另外一种方法来读取uint32_t值使用类似于下面的代码?

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


解决方案

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

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

I'd like to read uint32_t integers from a file using the code below. ifstream only accepts a pointer to a char array. Is there another way to read uint32_t values using a code similar to the below?

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

解决方案

Use a cast, e.g.:

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

(Interpreting any object as an array of characters is expressly allowed, precisely for the purpose of I/O.)

这篇关于从文件读取uint32_t值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆