Thread: Binary read from file

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    12

    Question Binary read from file

    Hi!
    I'm trying read data from binary file.

    File looks in hex editor:
    12 1A 15 00 AD AE 11 00 16 00

    I use getc to recive data.

    Code:
    ...
    
    char num_1, num_2;
    int mask, i,;
    ...
    
    data_file = fopen(where, "rb"); 
    
    ...
    
    for(i=1;i< end_file ;i=i+2)
    		{
    		         num_1 = getc (data_file);
    		         num_2 = getc (data_file);
    	
                      mask= 0x0000000000000000;	
    
                     printf ("i=%d mask=%016x mask=%d num_1=%016x num_2=%016x \n", i, mask,  mask, num_1, num_2);
                   }
    ...
    But I'm confused with results:
    i=0, mask=0000000000000000, mask=0, num_1=0000000000000012, num_2=000000000000001A
    i=1, mask=0000000000000000, mask=0, num_1=0000000000000015, num_2=0000000000000000
    i=2, mask=0000000000000000, mask=0, num_1=00000000FFFFFFAD, num_2=00000000FFFFFFAE
    i=3, mask=0000000000000000, mask=0, num_1=0000000000000011, num_2=0000000000000000
    i=4, mask=0000000000000000, mask=0, num_1=0000000000000016, num_2=0000000000000000

    Why for i=2 num_1 is FFFFFFAD instead AD ??

    Daniel

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    getc returns int (you implicetly conver it to char)

    char could be unsigned or signed - in your case it is signed, so when it is converted back to int and then printed - you see what you see.

    Try to avoid implicit conversions
    For example something like
    Code:
    unsigned char num[2];
    size_t read;
    
    ...
    
    read = fread(data_file,1,2,num);
    if(read == 2)
    {
       fprintf("&#37;02X %02X", (unsigned int)num[0], (unsigned int)num[1]);
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It should, however, be sufficient to change the char to unsigned char - it will then be automatically converted to int in the call to printf, but since unsigned char fits without touching the sign bit in an int, it is fine to do that.

    --
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by matsp View Post
    It should, however, be sufficient to change the char to unsigned char - it will then be automatically converted to int in the call to printf, but since unsigned char fits without touching the sign bit in an int, it is fine to do that.

    --
    Mats
    As long as getc does not returns EOF
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    As long as getc does not returns EOF
    Of course.

    --
    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.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    12
    Thats right.

    Thanks for help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Read in binary file ( pointers )
    By Giant in forum C Programming
    Replies: 41
    Last Post: 06-23-2005, 04:54 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM