Solved

Searching Data in binary file

Posted on 2004-08-01
6
233 Views
Last Modified: 2010-04-01
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]

                cin.getline(string,20);
     cin.getline(string,20);
     strupr(string);

and then I read from file and compare;
                file.read((char*)(&personModify),sizeof(personModify));

     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.
0
Comment
Question by:claracruz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Assisted Solution

by:meabed
meabed earned 150 total points
ID: 11687444
yeah .. this my assignment in the past .. try it .. it work fine .. ;)
#include <iostream.h>
#include <stdlib.h>
main()
{
 //
 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<<array[i]<<";";
 }
 cout<<" \n"<<"Number to find: ";
 cin>>number;
 while (sw == false && left <= right)
 {
  middle=(left+right)/2;
  if (number == array[middle])
  {
   sw=true;
   cout<<"number found.\n";
  }
  else
  {
   if (number < array[middle]) right=middle-1;
   if (number > array[middle]) left=middle+1;
  }
 }
 if (sw == false) cout<<"number not found.\n";
 system("Pause");
}
0
 
LVL 22

Accepted Solution

by:
grg99 earned 200 total points
ID: 11687643
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.

0
 
LVL 17

Expert Comment

by:rstaveley
ID: 11688237
> 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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:claracruz
ID: 11691768
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.


0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 150 total points
ID: 11692152
"Arbitrary" means any old size.

Imagine you have the following binary data file:

--------8<--------
Barny_Rubble________Fred_Flintstone_____Wilma_Flintstone____Clara_Cluz__________
--------8<--------

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.
0
 
LVL 5

Expert Comment

by:vilia
ID: 11826992
Hi claracruz,

Please let me know, have you found a solution or the question is open for answers?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question