fread'ing binary data

This is a discussion on fread'ing binary data within the Windows Programming forums, part of the Platform Specific Boards category; I'm writing some simple command-line shell code to interface with embedded hardware. In some cases, I need to move data ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    3

    fread'ing binary data

    I'm writing some simple command-line shell code to interface with embedded hardware.
    In some cases, I need to move data between the hardware and Windows data files.
    Where I need to interactively edit the data in a file, I'm using an external hex editor (NEO, in this case.)

    All that works fine, until I read from a file and manipulate the data in C, and then, only when encountering a <0d> <0a> sequence in the file.
    Even though I (think I) am opening the file in binary mode, the file system always collapses that sequence to simple <0a>, reporting one fewer byte read.

    Code:
    unsigned char buff[MAX_PACKET];
    FILE * fp;
    //
    // Creating file:
      fp = fopen(fname, "wb");
      fclose(fp);
    //
    // Reading file:
      fp = fopen(fname, "rb");
      nb = fread (buff, sizeof(unsigned char), MAX_PACKET, fp);
      fclose (fp);
    //
      printf ("%02x %02x %02x %02x %02x %02x %02x %02x\n", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5], buff[6], buff[7]);
    The external hex editor sees the data as expected, writes it, etc.
    How do I get around this "feature"?

    (I have also tried open() and read(), with no different results...)

    Thanks,

    Dave

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I created a simple hex file with eight bytes (0x08 through 0x0f). The output of your code as posted above was:
    Code:
    08 09 0a 0b 0c 0d 0e 0f
    Hence your claim that you are losing a byte upon read is incorrect.

    EDIT: Just noticed that you are concerned about 0d 0a. Update again in a minute. Okay: I added 0a after the 0d byte, and now I get
    Code:
    08 09 0a 0b 0c 0d 0a 0e
    so still not losing a byte. (I'm using MinGW on WinXP for comparison.)
    Last edited by tabstop; 07-24-2009 at 01:36 PM.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    3
    Quote Originally Posted by tabstop View Post
    Okay: I added 0a after the 0d byte, and now I get
    Code:
    08 09 0a 0b 0c 0d 0a 0e
    so still not losing a byte. (I'm using MinGW on WinXP for comparison.)
    Looks like it may be compiler/library dependent, then.
    I'm using Bloodshed Dev_Cpp, with the free Borland command line tools (BCC55) under WinXP Pro. The Dev_Cpp tree includes mingw32 \bin and \lib\ldscripts (not much in the latter.)
    Could I have the IDE configured wrong, and not using the right versions od some library?

    Thanks!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I doubt this is a compiler/library problem. Can you post complete, compilable code that demonstrates the problem you are seeing?

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    3
    Quote Originally Posted by bithub View Post
    I doubt this is a compiler/library problem. Can you post complete, compilable code that demonstrates the problem you are seeing?
    Not any more...
    I compiled the absolute minimum few lines to read and dump the buffer, and it worked fine.
    Went back to my full program, moved the declaration of buff[] out to a global, and when I recompiled, found a couple of places that now gave me warnings about argument type errors, that had not been warnings before(!).
    Fixed those, and everything works as I expected in the beginning!

    Thanks for the help - looks like a stupid error that slipped by the compiler.

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  2. How to write image data to binary PGM file format(P5)?
    By tommy_chai in forum C Programming
    Replies: 6
    Last Post: 11-03-2007, 11:52 PM
  3. Binary comparison
    By tao in forum Windows Programming
    Replies: 0
    Last Post: 06-28-2006, 01:10 PM
  4. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM
  5. ReadFile and binary data
    By alandrums in forum Windows Programming
    Replies: 1
    Last Post: 01-30-2002, 10:46 PM

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