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?
You probably want:
Seems OK to me,
fread(ptr, 1, 2, file)is even better. Orfread(ptr, sizeof(uint16_t), 1, file).I assume that
ptris 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.