0

Hello fellow stackoverflowers.

I'm doing a little hobby project in C code where I would like to do sone processing on .BMP files. So with me I use this wikipedia page Bmp file format

But very quickly I stumped upon a question. I'm trying to read the first 14 bytes of the file, the bitmap file header. But when when I print the bytes read they are only of 8 bytes length? Is this because the other bytes are zero or am I doing something wrong?

From GDB:

(gdb) p pImageHeader 
$3 = 0x602250 "BMz\270\v"
(gdb) x pImageHeader 
0x600x602250:   0xb87a4d42

Here is my code:

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char* argv[])
{
// If arguments are less than 2, just quit and tell user how do use the god damn program!
if (argc < 2)
{
    printf("Usage : %s xxxx.bmp \n", argv[0]);
    exit(1);
}

// Create file pointer and attach it to argument file location
FILE* pImageFile;

pImageFile = fopen(argv[1], "r");

// if file doesn't open, quit the program   
if (pImageFile == NULL)
{
    printf("Some error occured openening file : %s \n", argv[1]);
    exit(1);
}

char* pImageHeader;
pImageHeader = malloc(sizeof(char) * 14);
if (pImageHeader == NULL)
{
    printf("Error asking for RAM \n");
    exit(1);
}

const int HEADER_SIZE = 14; 
size_t bytes_read = fread(pImageHeader, 1, HEADER_SIZE, pImageFile);

if (bytes_read < HEADER_SIZE)
{
    printf("Something went wrong reading file header! \n");
    exit(1);
}

int i = 0;
    for (i; i != 14; i++)
{
    printf("%02X ", pImageHeader[i]);
}

    printf(" \n");
return 0;
}

EDIT: Change the source code to check up the amount of bytes actually read. It passes so it reads 14 bytes.

MrSykkox
  • 528
  • 4
  • 14
  • 1
    You should check the return value of `fread` to see how many elements were read. Also, consider including a minimal sample/example of your code in your question so people don't have to follow your link. – ilent2 Jun 22 '14 at 16:50
  • Thank you the quick response, I'll update my code asap. – MrSykkox Jun 22 '14 at 16:54
  • Also see http://stackoverflow.com/q/605845/2372604 about casting from `malloc`. This could be your problem too, if you don't have `stdlib.h`. – ilent2 Jun 22 '14 at 16:55

1 Answers1

3

Your code is fine. Your debugging commands are not.

(gdb) p pImageHeader 
$3 = 0x602250 "BMz\270\v"

Since pImageHeader is a char*, GDB assumes it is a NUL-terminated "C-string", and when you say p, it tries to print it as such. If there is a NUL byte in those first 14 bytes, GDB will stop printing characters there.

Instead, try x/14xb pImageHeader . This will print 14 hex bytes from the pointer pImageHeader.

Of course, you should also consult the documentation for GDB, specifically 8.5 Examining memory.

My SSCCE:

#include <stdlib.h>
#include <string.h>
int main()
{
    char *p = malloc(20);
    memcpy(p, "Test\0Worked\0", 12);
    return 0;
}

GDB:

(gdb) print p
$1 = 0x601010 "Test"                        <--- See, this failed!
(gdb) x/12xb p
0x601010:   0x54    0x65    0x73    0x74    0x00    0x57    0x6f    0x72
0x601018:   0x6b    0x65    0x64    0x00
(gdb) x/12cb p
0x601010:   84 'T'  101 'e' 115 's' 116 't' 0 '\000'    87 'W'  111 'o' 114 'r'
0x601018:   107 'k' 101 'e' 100 'd' 0 '\000'
(gdb) 

See also:

Community
  • 1
  • 1
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Thank you I see your are right! :) Yeah I must admit that I suck using the gnu debugger. I'm used to use the Visual Studio IDE. Again thank you! – MrSykkox Jun 22 '14 at 17:18