Thread: dump the buffer read from binary file

  1. #16
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    Adak, have you taken a look at my bin file?

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No. My hex editor has taken a dump. I'll have to grab another one.


    Edit:
    ===
    Ok, I'm back with a hex editor, and all is well. Have a question for you though.

    In the binary file, what's the best way to locate the mark in the file?

    With just one record, I can't make a general rule.

    Rest of the program is great - you'll be smiling.
    Last edited by Adak; 11-22-2009 at 03:40 AM.

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak View Post
    I'm much too old to be impressed by how correct you think you are.
    I'm not here to impress you, or anyone for that matter. I do however feel it necessary to point out the right way to do something. You don't teach that it's fine to modify string literals, when it is undefined behavior.

    It's not my fault you use an antiquated compiler that didn't handle it right in the first place. That's like advocating fflush(stdin) just because it works on <random compiler>.


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

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by quzah View Post
    I'm not here to impress you, or anyone for that matter. I do however feel it necessary to point out the right way to do something. You don't teach that it's fine to modify string literals, when it is undefined behavior.

    It's not my fault you use an antiquated compiler that didn't handle it right in the first place. That's like advocating fflush(stdin) just because it works on <random compiler>.


    Quzah.
    Don't worry, you're not impressing me, atm.

    I don't teach that it's fine to modify string literals. I've referenced Laserlight's post clarifying that point, three times now, in this thread.

    How many times will it take before you read her post, and understand why I referenced it? How many more posts will it take before you quit playing your violin?

  5. #20
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    In the binary file, what's the best way to locate the mark in the file?
    it is this way:
    you read first 3 bytes(key).
    then you read N bytes until you reach \0 terminator. (Name)
    after that, you count 20-N bytes(ignore them).
    and then read 4 bytes (Mark)

    Rest of the program is great - you'll be smiling.
    which program do you mean? my code?

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The input portion of it.

    20 - N bytes. May have to do some tweaking on that part of it.

    I have to run just now, but I'll post it up when I return.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If this is a binary file with fixed lengths records, why do you keep trying to use fscanf? Why aren't you just using fread to read 27 bytes at a time, or 3 bytes, 20 bytes, 4 bytes?


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

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Because I wanted to see if you were awake, of course!

    Code:
    /* help for r00t on cprogramming forum */
    
    #include <stdio.h>
    #include <string.h>
    
    #define KeyLen 4
    #define NameLen 21
    
    typedef struct
    {
      char key[4];
      char name[21];
      int Mark;
    }rec;
    
    int main(void) {
      size_t i, j;
      FILE *fp;
      
      rec recs[3];
    
      /* The fields in the records are fixed width, (the fields are padded
         whenever the data doesn't fill it all the way). I've left them
         at their fixed widths.
    
         No subtraction to find the Mark is needed that way.
      */
    
      if((fp = fopen("mu1.bin", "rb")) == NULL) {
        printf("\n Error opening the input file - terminating program\n");
        return 1;
      }
      i = j = 0;
      do {
        j = fread(recs[i].key, KeyLen - 1, 1, fp);
        if(!j) 
          break;
        fread(recs[i].name, NameLen, 1, fp);
        fread(&recs[i].Mark, 4, 1, fp);
        recs[i].key[KeyLen - 1] = '\0';
        recs[i].name[NameLen - 1] = '\0';
        printf("#%d: %s %s %d\n", i, recs[i].key, recs[i].name, recs[i].Mark);
        ++i;
      }while(1);
    
      fclose(fp);
      printf("\n\n\t\t\t     press enter when ready");
      i = getchar();
      return 0;
    
    }
    I only tested this on the one record you posted to rapidshare.
    Last edited by Adak; 11-23-2009 at 12:21 AM.

  9. #24
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    Hey thanks a lot, Adak,
    now i know how fread() works!

    Thanks everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read a file of commands and execute them
    By surlyTomato in forum C Programming
    Replies: 5
    Last Post: 08-20-2009, 11:32 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. efficient binary file read
    By Marv in forum C Programming
    Replies: 15
    Last Post: 11-19-2007, 04:42 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM