Domanda

I am trying to learn more about binary files, so I started with HexEdit, and I manually wrote a file and created a template for it. Here is my work:

enter image description here

Now, I started working on a console application in C++ Win32 to read the contents in that file and make them look friendly. Here is part my code:

typedef unsigned char BYTE;

long getFileSize(FILE *file)
{
    long lCurPos, lEndPos;
    lCurPos = ftell(file);
    fseek(file, 0, 2);
    lEndPos = ftell(file);
    fseek(file, lCurPos, 0);
    return lEndPos;
}

int main()
{
    const char *filePath = "D:\\Applications\\ColorTableApplication\\file.clt";
    BYTE *fileBuf;          // Pointer to our buffered data
    FILE *file = NULL;      // File pointer

    if ((file = fopen(filePath, "rb")) == NULL)
        printf_s("Could not open specified file\n");
    else {
        printf_s("File opened successfully\n");
        printf_s("Path: %s\n", filePath);
        printf_s("Size: %d bytes\n\n", getFileSize(file));
    }

    long fileSize = getFileSize(file);

    fileBuf = new BYTE[fileSize];

    fread(fileBuf, fileSize, 1, file);

    for (int i = 0; i < 100; i++){
        printf("%X ", fileBuf[i]);
    }

    _getch();
    delete[]fileBuf;
        fclose(file);   // Almost forgot this 
    return 0;
}

(I provided that much code because I want to be clear, to help you get the idea about what I am trying to do)

First of all, I need to get the first 14 bytes and write them in the console as text, and then, in a for I need to write something like this for each color:

black      col_id = 1; R = 00; G = 00; B = 00;
red        col_id = 2; R = FF; G = 00; B = 00;
etc...

How can I read and translate these bytes?

È stato utile?

Soluzione

It is correct as you have it to write out the 14 bytes.

a technique is to create a struct with the layout of your records, then cast e.g. (C-style)

typedef struct
{
  char name[10];
  long col_id; 
  unsigned char R;
  unsigned char G;
  unsigned char B;
} rec;

rec* Record = (rec*)(fileBuf + StartOffsetOfRecords);

now you can get the contents of the first record

Record->name, ...

getting next record is just a matter of moving Record forward

++Record;

You could also have a struct for the header to make it more convenient to pickout the number of records, it is good to use stdint.h in order to get well defined sizes. also to pack structures on byte boundary to make sure no padding is done by the compiler i.e. #pragma pack(1) at the top of your source.

typedef struct 
{
  char signature[14];
  uint32_t tableaddress;
  uint32_t records; 
} header;

typedef struct
{
  char name[10]; 
  uint32_t col_id;
  unsigned char R;
  unsigned char B;
  unsigned char G;

} rec;

so instead when you read you could do like this

header Header;
rec* Record;

fread(&Header,sizeof(header),1,file);
fread(fileBuf,1,fileSize,file);

Record = (rec*)(fileBuf); // first record can be accessed through Record
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top