?
Solved

Searching Data in binary file

Posted on 2004-08-01
6
Medium Priority
?
235 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 600 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 800 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 600 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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++.
Suggested Courses

770 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