简体   繁体   中英

How to read file header in c++

I want to write a program in C++ so that I can read a file with a file header that has 3 bytes. 1 byte for 1 variable.

I want to define a struct in C++ with 3 variables and all of them has 1 byte so that I can read value from file to these three variables.

My idea is this:

struct header{
    datatype a;
    datatype b;
    datatype c;
}

Then I can:

FILE *fp=fopen(fileName,"rb");
header head;
fread(&head, sizeof(header),1,fp);

Those variables are used for calculating and their range is from 0-255. What datatype I can use in c++?

<cstdint>包含的uint8_t用于具有1个字节长度的datatype

您可以使用char ,恰好是1个字节。

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