简体   繁体   English

在C ++中使用uint16?

[英]fread-ing uint16 in C++?

I'm trying to read use fread to read in values from an external file in C++. 我正在尝试使用fread来读取C ++中外部文件的值。 The values are stored as uint16's, which does not seem to exist in C++. 这些值存储为uint16,这在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. 我做了一些谷歌搜索,发现人们使用typedef制作自己的uint16,但我也想知道我是否可以使用fread(ptr,uint8,2,file)读取两个uint8来存储在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. 固定大小的整数类型(如uint16_t<stdint.h>头中定义。 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. 好像对我来说, fread(ptr, 1, 2, file)甚至更好。 Or fread(ptr, sizeof(uint16_t), 1, file) . 或者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 ). 我假设ptr是一个指向你想要存储数据的位置的指针,而不是变量本身(如果是这样 - 使用&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是两种在这种情况下可以帮助你的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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