shw*_*eph 8 c++ pointers fstream reinterpret-cast
我正在编写一些使用fstream read()函数的代码,这个函数需要一个char*作为缓冲区.稍后,我想使用此缓冲区中的字节作为无符号字符,因此我要么必须:1.将缓冲区声明为char*,然后为每个元素执行static_casts,2.声明缓冲区作为unsigned char*然后在我将它传递给read函数时执行reinterpret_cast,或者3.将缓冲区声明为char*并创建一个用于访问缓冲区作为unsigned char的转换指针.
这是一个片段:
char* buf = new char[512];
unsigned char* ubuf = reinterpret_cast<unsigned char*>(buf);
fstream myfile;
myfile.open("foo.img");
myfile.seekg(446);
myfile.read(buf, 16);
//myfile.read(reinterpret_cast<char*>(buf), 16);
int bytes_per_sector = ubuf[1] << 8 | ubuf[0];
...
Run Code Online (Sandbox Code Playgroud)
我喜欢这种方式,因为我只需要转换一次,我可以只访问缓冲区作为任何一种类型,而不是每次都进行转换.但是,这是一个好习惯吗?有没有什么可以在这里出错?使用reinterpret_cast让我有点紧张,因为我通常不会使用它,而且我被告知要多次小心它.
在这种情况下,reinterpret_cast很好,原因有两个:
(签名)char和unsigned char类型必须具有相同的"表示和对齐".这意味着数据不会有差异(它将是每位精确位数),或者解释缓冲区的时间长度.
文件读取功能通常char*仅用作通用数据访问类型.它们无法使用,void*因为类型void具有特定的未定义长度和表示.char,但是,确实如此.所以他们可以用它来读/写一系列字节.
实际上,文件函数通常用于将数据重新解释为/从其他内容重新解释.它允许你有一个像这样的结构
typedef struct structSomeStruct
{
char name[8]; // a basic, fixed length, string
unsigned long i; // a 32 bit value
float x, y, z, w;
} SomeStruct;
Run Code Online (Sandbox Code Playgroud)
要么
class SomeStruct
{
public:
char name[8];
unsigned long i;
float x, y, z, w;
SomeStruct()
{
// ...
}
};
Run Code Online (Sandbox Code Playgroud)
并使用以下内容将其存储到文件中:
SomeStruct st;
// populate the st data structure
// file.write(char* start_of_data, size_t number_of_bytes);
file.write(reinterpret_cast<char*>(&st), sizeof(SomeStruct));
Run Code Online (Sandbox Code Playgroud)
并以类似方式阅读.