I'm having a problem with retrieving records from my file just started programming over a month a go please help this is for my class project.
I need to do delete,update,query,and add to binary file and prompt the user to enter which record they need to retrieve and i started adding.please if you could give me an example of each i would be very gratefully.my project needs to be submmitted by 28/7/08.

here is my code:

#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<windows.h>
#include<conio.h>

using namespace std;

class houses
{
    int house_number;
    int number_of_rooms;
    string furnished;
    double cost;



public:
   /* houses()
    {
        house_number = 0;
        number_of_rooms = 0;
        furnished = "";
        cost = 0;
        occupied = "";
    }*/

void get_house()
{
    cout<<"Please enter the house number(integer data only): ";
    cin>>house_number;
    cout<<"Please enter the number of rooms(integer data only): ";
    cin>>number_of_rooms;
    cout<<"Please enter the cost of the house: ";
    cin>>cost;
    cout<<"Please indicate if the house is furnished(yes,no): ";
    cin>>furnished;
}
void show_house()
{
    cout<<"house number: "<<house_number<<endl;
    cout<<"number of rooms: "<<number_of_rooms<<endl;
    cout<<"cost of the house: "<<cost<<endl;
    cout<<"Is house the furnished: "<<furnished<<endl;
    }
void housewrite()
{
    houses h;
    ofstream out("house.bin",ios::app);
    if(out == NULL)
    {
        for(int i = 0; i < 15;i++)
        {
            cout<<"..";
            Sleep(200);
            }
        cout<<"......FILE COULD NOT BE OPENED";
        }
    else
    {
        get_house();
        out.write((char *)&h,sizeof(h));
        out.close();
       for(int i = 0; i < 15;i++)
       {
           cout<<"..";
           Sleep(200);
           }
        cout<<".....FILE WRITTEN SUCESSFULLY";
        }

    }
void display_house()
{
    int house_temp ;
    houses h;
    ifstream in("house.bin",ios::in|ios::binary);

    if(in == NULL)
    {
        for(int i = 0; i < 15; i++)
        {
            cout<<"..";
            Sleep(200);
        }
        cout<<"..........FILE COULD NOT BE OPENED";
        }
    else
    {
        do
        {
        cout<<"ENTER HOUSE NUMBER: ";
        cin>>house_temp;
        in.seekg(house_temp*sizeof(h),ios::beg);
        in.read((char *)&h,sizeof(h));

        if(house_temp == h.house_number)
            {
                h.show_house();
                break;
            }
            else
            {
                cout<<"HOUSE NUMBER DOES NOT EXIST.......";
                break;
            }

        }while(!in.eof());
        }in.close();
    }
    };


int main()
{
    houses obj;
    obj.housewrite();
    getch();
    system("cls");
    obj.display_house();

    return 0;
    }

The idea is generally there; however, there are some things you need to look at:

1) Reading & writing the entire object out - that can be problematic. What if, the first time you write out a "houses" object, the "furnished" string is set to "no"? Then the second, it's set to "yes"? The object may not look like you expect internally. Better to write each variable out knowing what length each is:

out.write((char *)&house_number, sizeof(int));
...

Then, in your display_house() function, do the same when reading in:

in.read((char *)&house_number, sizeof(int));
...

2) your total record size (needed for the seekg operation) will now no longer be sizeof(h) , but the combined size of each of your elements (allow 4 bytes for the string, since you are storing "yes" or "no").

3) Internal use of the object you're defining. I guess it can be OK, but not like this (in the housewrite() method):

houses h;
...
get_house();
out.write((char *)&h,sizeof(h));
out.close();

The method "get_house()" loads the data into the object variables of the current instance, but then you attempt to write out "h" - an uninitialized local version of your object. You'll be fixing that anyway if you follow the steps in 1 & 2, above.

4) The method you're using to locate records is a combo of relative record number, and "house_number" the user enters, which can be easily broken. You use the house_number to perform the seek, then check the record you read in to see if the house number from the file matches what the user typed. If you run the program for the first time, enter a "7" as the house number and then try to search on that number, you'll seek past the end of file. Either use the relative record number, or just start at the top and read each record, attempting to match the house number the user entered. Choose one or the other.

See if these help and let me know how it comes out.

Where are you having the problem?
What does the program do wrong?
What makes it wrong?
What should it do instead?

When asking for help it's necessary to explain the problem, not just toss 3 ambiguous sentences together without punctuation making it hard to decipher your meaning.

Never write objects with std::string members (see furnished) in address-sizeof style. It's wrong to write (and read) individual std::string type members with this C-like mechanics. The size in bytes of std::string ( sizeof(std::string) is well-defined and CONSTANT value. It does not include true size of contained text. The pointer to std::string object does not refer to the contained text. You may write std::string object in a such manner but never get it back (w/o possible program crash).

So if you want to compute relative file offset for i-th record, use only basic types members with fixed length in your record structure type.

This question has already been answered. Start a new discussion instead.