Read file header information

I have followed the tutorial and thought I would attempt to modify the Input/Output with files example to read some information about a file. For instance bytes 0-12 contain title, fairly easily achieved this with cout in a loop. But I want to read bytes 128-132 and have them as one hex value but I have tried to work around this but keep getting invalid conversion errors or invalid types for array subscript.

Can anyone suggest a way to do this or what I am doing wrong please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>

using namespace std;
ifstream::pos_type size;
char * memblock;

int main () {
    int info;
  ifstream file ("c:\\test.xxx", ios::in|ios::binary|ios::ate);
  if (file.is_open())
  {
    size = file.tellg();
    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();

    cout << "Header =\t" ; //first 12 bytes
    for (int count=0; count<12; count++)
    {
        cout << memblock[count] ;
    }
    cout << endl;

    cout << "Code =\t\t"; //bytes 12 to 16
    for (int count=12; count<16; count++)
    {
        cout << memblock[count];
    }
    cout << endl;

    for (int count=128; count<=131; count++)
    {
        for (int loop=0; loop<=4; loop++)
        {
            info[loop] = memblock[count];
        }
    }
        cout <<  info <<endl;

    cout << endl << endl << "the complete file content is in memory" << endl;

    delete[] memblock;
  }
  else cout << "Unable to open file";
  return 0;
}
reading 128-132 in one go... that is 5bytes.. correct..!!

read five bytes in a unsigned char array and combine them.
give you an example for 4bytes and do this for any number of bytes.
lets say your file has 12345678 (0xBC614E) (00000000-10111100-01100001-01001110)

if you read these four bytes your array will get this:
1
2
3
4
5
6
7
8
9
10
unsigned char arr[10];
fread(arr, sizeof(unsigned char), 4, fp);

//the array will have these values
//arr[0]= 0;
//arr[1]= 188;
//arr[2]= 97;
//arr[3]= 78;

int value = arr[0]<<24 | arr[1]<<16 | arr[2]<<8 | arr[3];



what say??
About this piece of code:

1
2
3
4
5
6
7
    for (int count=128; count<=131; count++)
    {
        for (int loop=0; loop<=4; loop++)
        {
            info[loop] = memblock[count];
        }
    }


You can't index "info", it is not an array (or even a pointer).
You have a two-dimensional loop structure for a one-dimensional problem.
(And the inner loop would execute 5 times anyway.)
You mean something like this:

1
2
3
4
5
    char* pInfo = reinterpret_cast<char*>(&info);
    for (size_t i=128; i<132; i++)
    {
        *pInfo++ = memblock[i];
    }


(Or should they be read in reverse order? Which endianness?)

Also, you don't need to read the whole file into a buffer first. You can just seekg to the positions you want and read the data from the file.
Thanks guys, yes 128-132 is 5 bytes sorry my mistake.

Big endianness in this case but it is something I need to consider.
Topic archived. No new replies allowed.