Solved

Searching Data in binary file

Posted on 2004-08-01
6
218 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

828 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