Like Tree1Likes
  • 1 Post By rags_to_riches

Search in binary file

This is a discussion on Search in binary file within the C Programming forums, part of the General Programming Boards category; Hello i have a structure in binary file and i want to make a search option by surname for example. ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    58

    Search in binary file

    Hello i have a structure in binary file and i want to make a search option by surname for example. I want to ask can somebody could post examples how to search by surname or something like this.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    How are the structs stored in the file? Are they all of equal size? Is the string part of the struct or only a pointer, if so, where are the strings? One approach for storing the structs is, one record per line, then you can use fgets/sscanf for example.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    This is structure
    Code:
    struct student_list {
           int number;
           char student_code[20];
           char surname[50];
           char name[50];
           double sholarship;
    };
    This is how i store data
    Code:
    case 1:
    {          
        if ( ( fp = fopen( "list.dat", "rb+" ) ) == NULL )
         {
              printf("File does not exist. \n");
         }
         else
         {
         printf("Enter student number "); 
         scanf( "%d", &student.number );
         printf("Enter student code, surname, name and scholarship \n");
         fscanf(stdin, "%s%s%s%lf", student.student_code, student.surname, student.name, &student.scholarship);
         fseek( fp, ( student.number - 1 ) *  sizeof( struct student_list ), SEEK_SET );
         fwrite( &student, sizeof( struct student_list ), 1, fp );         
         }
         fclose( fp );
         break;
    }
    And this is how i tried to search
    Code:
    case 2:
                           {
                                 printf("Enter surname \n");
                                 scanf("%s",sur);
                                 while ( !feof( fp ) ) 
             { 
                     fread( &student, sizeof( struct student_list ), 1, fp );
                     if ( student.surname == sur )
                      {
                        printf( "%-10d%-10s%-16s%-11s%10.2f\n",student.number, student.student_code, student.surname, student.name, student.scholarship );
                        }
                        system("pause");
                        }

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    That may or may not work, depending on if the struct is padded or not. You can't really make any assumptions about it, it can differ among compilers and so on.

    But, does it work? What size does sizeof( struct student_list ) return? Does it match the sum of the member sizes?

    Besides that, you can search the entire file by wrapping fread in a while loop and overwrite student in each iteration.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    That is does not work it returns nothing when i try to search.

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Salem likes this.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In C you can't compare strings directly to each other, so this will never work:
    Code:
    if ( student.surname == sur )
    You need to include the header file string.h in your program, and then do something like this:
    Code:
    if ((strcmp(student.surname,"sur"))==0)
    The return from strcmp will either be a negative number (indicating student surname was larger than "sur"), a zero (indicating they are the same), or a positive number (indicating that student.surname was smaller than "sur").

    So it's like a teeter-totter, with the pivot right between the two strings. If the left side is larger (heavier), the left side of the teeter-totter goes down, and the negative numbers result. If the right side is larger (heavier), the right side of the teeter-totter goes down, but now you're on the positive side of the the pivot point, so the result is a positive number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference Between A Linear Search And Binary Search
    By ImBack92 in forum C Programming
    Replies: 4
    Last Post: 05-12-2011, 08:47 AM
  2. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 01:12 PM
  3. How to search within a binary file
    By khpuce in forum C Programming
    Replies: 17
    Last Post: 04-12-2005, 05:23 AM
  4. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 09:18 AM
  5. Binary search on a HUGE file.
    By Appoloinus in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2003, 11:29 PM

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