How to search within a binary file

This is a discussion on How to search within a binary file within the C Programming forums, part of the General Programming Boards category; Hello everybody, I know how to search within a text file but I fail to do the same with a ...

  1. #1
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165

    How to search within a binary file

    Hello everybody,

    I know how to search within a text file but I fail to do the same with a binary file. I can load the entire binary datafile using fread() but I am trying to load only one record (based on users' input like order no.) at a time.

    Can anyone please tell me the command to use for loading a specific record (not the entire data file!) from a binary file. I did a board search but couldn't fine anything.

    Thank you

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Why is that problem?
    For example, let's say you have records something like this:
    Code:
    typedef struct info
    {
            char name[21];
            int order_num;
    }info;
    So you have simple structure and consisting of customer name and order number. Let's assume you have records like this stored in a binarny file. All you need to do is to open file in binary mode for reading, read sizeof info bytes (assuming records are written to the file that way) store it to a temp variable and check if that record matches certain criteria.

    Here look at this code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct info
    {
            char name[21];
            int order_num;
    }info;
    
    int main(void)
    {
        info cust[3];
        char buf[21];
        int i,on;
        info tmp;
        FILE * fp;
        /* first time uncommented*/
       /* for (i = 0; i < 3; ++i)
        {
            sprintf(buf, "Cust. no. %d",i+1);
            cust[i].order_num = i + 1;
            strcpy(cust[i].name, buf);
        }  
        fp = fopen("test.bin", "wb");
        
        for (i = 0; i < 3; i++)
        {
            fwrite(&cust[i], sizeof(info), 1, fp);
        }*/
        
        /* first time commented*/
        fp = fopen("test.bin","rb");
        
        on = 2; /*we search order no. 2*/
        
        while( (fread(&tmp, sizeof(info), 1, fp) == 1) && (tmp.order_num != on));
        if (tmp.order_num == on)
        {
            printf("%s",tmp.name);
        }
        
    }
    All error checking I'm leaving to you.
    Just for demonstration purpose!
    I hope this helps!
    Last edited by Micko; 04-11-2005 at 08:14 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Also, if you want to jump to a specific record in Micko's example, look up the fseek() function.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165
    Thanks Micko.

    I've been trying similar methods without any success

    Say, here's my structure

    Code:
    #define MAX_ITEM 10
    
    typedef struct {
    
         int stk_num,items_in_stk;
         float price;
         char desc[26],ord_ref[9];
    } record;
    
    record List[MAX_ITEM];
    
    FILE *datafile;
    and here's the way I'm trying to read and load a specific record

    Code:
       datafile = fopen("mydatafile.dat","rb");
    
        sn = 2; 
        while ((fread(&List, sizeof(List), 1, datafile) == 1) && (List->stk_num != sn)) ;
    
        if (List->stk_num == sn)
        {
            printf("%d",List->stk_num);
            printf("%d",List->item_in_stk);
    
        }
    The problem I'm having is with the loop I guess. It seems to go through only once ! I've been trying different variations but still no luck

    Edit : I also looked at fseek() but don't know how to use it in this case.
    Last edited by khpuce; 04-11-2005 at 09:25 AM.

  5. #5
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165
    Ok...now I think its not the loop that's causing the problem. I mean even if I run the following statement

    Code:
    while ((fread(&List, sizeof(List), 1, datafile) == 1) && (List->stk_num != sn)) ;
    more than once, the result is same.
    Code:
    if (List->stk_num == sn)
    Here, List->stk_num is not changing and seems to point the first record only

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Something is fishy with your code. Please write elements of array to file one by one and also read one by one.
    Please explain to me what is this
    fread(&List.....) what List means and what List->stk_num means?
    When you start explaining to me start with what List is!!!
    i know what is problem, but I'll try to navigate you to find out!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  7. #7
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165
    Thanks for the hint Micko. I think I can see the problem now. It sould be
    Code:
    while ((fread(&List, sizeof(record), 1, datafile) == 1) && (List->stk_num != on))  ;
    Shouldn't it ?

  8. #8
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Well, please explain me!
    1. What List is???
    2. How records are stored/written to a file?
    3. What is List->stk_num?
    4. Is List a pointer, if so, what kind of....?

    I'm waiting for you to explain me how you want to read records and how you coded it...
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    The -> operator is not supported by C. Maybe this should be moved to the C++ forum.

  10. #10
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Quote Originally Posted by joshdick
    The -> operator is not supported by C. Maybe this should be moved to the C++ forum.
    What???
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  11. #11
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    The -> operator is not supported by C. Maybe this should be moved to the C++ forum.
    It is defined, and is very widely used. Indispensible, some would say.
    Last edited by Jez; 04-11-2005 at 02:01 PM. Reason: Spelling mistake

  12. #12
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    When did that get added to C? I really thought that was just C++.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Quote Originally Posted by joshdick
    When did that get added to C? I really thought that was just C++.
    Something like 1978.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Quote Originally Posted by itsme86
    Something like 1978.
    My mistake. I misread a reference book.

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @khpuce: In your latest 2 posts, containing sample code, List is defined as an array. If you're only wanting to load 1 record into memory, or rather, only ever have one record in memory, you don't want an array. Instead, you want 1 struct, much like Micko did in their example with the variable named tmp.

    You basically read 1 structures worth of data into memory, check to see if its the one you want, if not, load the next lot of data over the top of it and test again. No array needed.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. Print file in binary mode
    By vikernes in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2006, 12:43 AM
  5. copying binary file
    By samc2004 in forum C Programming
    Replies: 5
    Last Post: 12-09-2003, 01:34 PM

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