fread returns strange values

This is a discussion on fread returns strange values within the C Programming forums, part of the General Programming Boards category; I have this code segment meant to read the header from a standard ms paint bitmap. It reads most of ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    21

    fread returns strange values

    I have this code segment meant to read the header from a standard ms paint bitmap.
    It reads most of the stuff fine but returns strange values for 0xF9 0xC2 0xD8. (these are actually stored in a char data type but

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      FILE *bmp;
      char *tmphead;
      int i;
      
      bmp = fopen("image.bmp","rb");
      if ( bmp == NULL )
        exit(0);
    
      fread(tmphead, 1, 0x35, bmp);
      
      for (i=0;i<0x35;i++)
        printf("%X %u\n",tmphead[i],tmphead[i]);
    
      fclose(bmp);
      scanf("%d");
    
      return 0;
    }
    and returns this

    Code:
    42 66
    4D 77
    E 14
    37 55
    2 2
    0 0
    0 0
    0 0
    0 0
    0 0
    36 54
    0 0
    0 0
    0 0
    28 40
    0 0
    0 0
    0 0
    FFFFFFF9 4294967289                   << ? this is not right (should be F9)
    0 0
    0 0
    0 0
    FFFFFFC2 4294967234                   << ? this is not right (should be C2)
    0 0
    0 0
    0 0
    1 1
    0 0
    18 24
    0 0
    0 0
    0 0
    0 0
    0 0
    FFFFFFD8 4294967256                   << ? this is not right (should be D8)
    36 54
    2 2
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    The char is only supposed to hold 8 bits and this is not an 8 bit number.
    I have compiled this with gcc distributed through Dev-Cpp package.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    tmphead points nowhere (is uninitialized)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably want to make it an unsigned char as well - since that's why you get a load of f's at the beginning of the number - it's treated as a 32-bit signed number. Anything >= 0x80 in a signed char is negative, and when expanded to 32-bit will have 6 F at the beginning.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    It declares a char as 32bit?
    char is supposed to be 8 bit signed I thought.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He's trying to say that the format specifier you've chosen doesn't match what we think you're actually trying to read. Furthermore, you don't have any space allocated for your pointer, so you're reading into some random spot in memory. That's bad.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    yeah I see

    I always post before looking at my code

    thanks

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also bear in mind that ALL integer types smaller or equal to int automatically get converted to int size. So if you pass a short or char to printf [or any other function that takes variable number of arguments] then the value will be int-sized. It doesn't matter if you use %c, %d, %u or some other format specicfier - the compiler may match up your arguments, but it doesn't look at the format string to determine what type to pass to printf.

    Likewise, float becomes double, always.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  2. 2d array and fwrite and fread
    By totalnewbie in forum C Programming
    Replies: 14
    Last Post: 01-10-2009, 02:45 PM
  3. Replies: 2
    Last Post: 11-19-2008, 01:36 PM
  4. adding ASCII values
    By watshamacalit in forum C Programming
    Replies: 1
    Last Post: 12-26-2002, 06:16 PM
  5. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 09:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21