• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

Searching Data in binary file

G'day Experts,

I have a name which is normally in the format firstname lastname and is stored in a binary file.
At the moment, i am only able to search thru the customer file by their firstname by using the following code

             //variable declared
               char string[20]


and then I read from file and compare;

     if (file.eof()) break; //if end of file then stop search

               if (personModify.compare(string))..........e.t.c

In this case if customer name is stored as Mike Morrison, search will return 0 if Mrrison is input criteria. but will return 1 if Mike is input Criteria, I would like to know how to make it serch for the last name instead.

Thank you.
3 Solutions
yeah .. this my assignment in the past .. try it .. it work fine .. ;)
#include <iostream.h>
#include <stdlib.h>
 int array[10]={1,2,3,4,5,6,7,8,9,10};
 int left=0,right=9,middle;
 int number,i;
 bool sw=false;
 cout<<"ARRAY: ";
 for (i=0;i<=9;i++)
 cout<<" \n"<<"Number to find: ";
 while (sw == false && left <= right)
  if (number == array[middle])
   cout<<"number found.\n";
   if (number < array[middle]) right=middle-1;
   if (number > array[middle]) left=middle+1;
 if (sw == false) cout<<"number not found.\n";
You can do it two ways:

(1) Change compare() method so it returns a "1" if the patern is ANYWHERE in the string, not just at the beginning.   There's an obvious way to do this with the strstr function() or its C++ equivalent.

(2)  Pass the last name to the compare() function.  Just look for the space character, and pass along the string starting at that localtion plus 1.

> sizeof(personModify)

Does this correspond to a known record size? If it is an arbitrary buffer size, beware that you may have the lastname spanning two reads.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

claracruzAuthor Commented:
Thank you for your replies, but I am stil as stuck as ever.

I like grg99's idea of how to do this, I can do it in VB but don't have a clue with c++.

I wonder if anyone could provide code for this, please??

I don't Know if > sizeof(personModify) is an arbitrary buffer size or not, what does arbitrary buffer size mean and how is this implemented.

"Arbitrary" means any old size.

Imagine you have the following binary data file:


Records in this example have a fixed length and are 20 bytes long.

If you read records into a 10 byte buffer, it would be unreasonable to expect to recognise the string "Flintstone" in the buffer.

A 20 byte buffer would be perfect, because it would read the whole record in.

A 30 byte buffer is also a problem because the first read gets Barny's record and the first 10 bytes from Fred's, which breaks up the string "Flintstone".

While you are not using [newline] delimited records, you need fixed sized records, and your buffer size should correspond to a whole number of those fixed sized records.
Hi claracruz,

Please let me know, have you found a solution or the question is open for answers?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now