简体   繁体   中英

fread-ing uint16 in C++?

I'm trying to read use fread to read in values from an external file in C++. The values are stored as uint16's, which does not seem to exist in C++. I did some googling and found people using typedef to make their own uint16, but I'm also wondering if I could just use fread(ptr, uint8, 2, file) to read two uint8's to be stored in ptr.

Does anyone have any insight to the best way of going about this?

Fixed-size integer types like uint16_t are defined in the <stdint.h> header. Include that and you'll be in business.

You probably want:

fread(ptr, sizeof(uint8), 2, file)

Seems OK to me, fread(ptr, 1, 2, file) is even better. Or fread(ptr, sizeof(uint16_t), 1, file) .

I assume that ptr is a pointer pointing to the location where you want to store your data, not the variable itself (if so - use &ptr ).

Reading two bytes at a time though may become a performance issue, consider reading into a memory buffer, and then parsing it.

#include <stdint.h>

应该为你解决

你可以使用一堆固定的大小,uint16_t和__int16是两种在这种情况下可以帮助你的东西。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM