Read uint32_t values from file

流过昼夜 提交于 2020-01-25 10:02:05

问题


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
}

回答1:


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.)



来源:https://stackoverflow.com/questions/23418818/read-uint32-t-values-from-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!