Solved

Searching Data in binary file

Posted on 2004-08-01
6
197 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

912 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now