Technical Help & Discussion > Website Design & Programming

c++ binary file ops

(1/4) > >>

davypipes:
hi guys!!!
ive been making this program thats supposed to store a number of records to a binary file.the problem ive encountered is with placing a record in a temporary object so that i can later on work on that data.i defined a class for this purpose as follows:

class temp
{
public:

 char farmerName[20];
  long idNumber;
  long accountNumber;
  char location[30];
 };

 and passed on the info from the file  as follows:

// function to edit the farmer details...
void Farmer::editFarmer()
{
fstream myfile("Farmer.dat",ios::in | ios::out | ios::binary) ;

   temp temprec;
   clrscr();
   cout<<"\t\tYOU HAVE CHOSEN TO EDIT A RECORD \n";
   cout<<"\t\t------------------------------------\n\n";

while(myfile>>farmerName>>idNumber>>accountNumber>>location)
 {

temprec.farmerName=farmerName;
//error raised on previous line:Lvalue required

temprec.idNumber=idNumber;

temprec.accountNumber=accountNumber;

temprec.location=location;
//error raised on previous line:Lvalue required

 }
}

the error ive been gettting is been raissed when i pass the farmername and the location variables which are both of type char.the rest seems to be working fine ...its just those 2 lines!!!

so ....anyone?? :-\


Sandra:
Sorry, I havent a clue about that stuff, hopefully someone will be along soon who may be able to help you out  ???

Simon:
Another job for Dack, me thinks.   ;)

Dack:
http://www.parashift.com/c++-faq-lite/serialization.html#faq-35.6

In other words, storing character data is not really recommended that way as you are never exactly sure where it's going to end.

Better to use the Read() and Write() functions for the character and location fields.

Assuming the farmerName and location variables are defined as per the structure i.e. char [ 20 ].

Remember that (in most instances) temprec.farmerName is actually an address - equivalent to &temprec.farmerName[ 0 ]. If you use that in your code you should see the error :)

i.e. you are trying to say:
&temprec.farmerName[ 0 ]= &farmerName[ 0 ]

You could use a memcpy(temprec.farmerName, farmerName, sizeof(temprec.farmerName)); to get rid of the error.

davypipes:
hmmmm.....
so i gueess lem me try out the write() and read() functions!! hopefully all will go well and thanks for the advice!! ;)

Navigation

[0] Message Index

[#] Next page

Go to full version