Data Error while reading rom file

Hi ,

I am trying to write a simple program ,

which has a class bank with 3 data members and 4 data function for various operation.

Here is the flow

Input from user -> write to file -> Read from file

But when i am reading all records from file i am getting the last record as many times as that of the number of input.

Please look into the below codes and advise :

Let me know if you need any clarification.

Thanking all in advance

/***************************************************************************/

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

class bank_record {
int customer_no;
char customer_name[10];
float balance;
public :
void input_record();
void write_to_file(bank_record*);
int read_from_file(bank_record*,int);

};
/*--------------------------------------------------------------*/

void bank_record::input_record( ){
cout << " \n Enter the customer details : 1. Number \n";
cout << "2. Name \n 3.Balance \n";
cin >>customer_no ;
gets(customer_name);
cin>>balance;
cout << " Inserting Customer Details.... \n";

}
/*------------------------------------------------------------------------------------------------------------------*/

void bank_record::write_to_file(bank_record* b1){
ofstream for_input("bank_customer_record.txt",ios::app|ios::binary);

if( ! for_input){
cout << "\n Sorry Dude Not Able To Enter The Record , The file is not open \n";
return;
}

else {
cout << "Writing to the file ...... \n ";
for_input.write((char *) &b1 , sizeof (class bank_record));
for_input.close();
}
}

/*------------------------------------------------------------------------------------------------------------------*/
int bank_record::read_from_file(bank_record *b2,int num){
int acct_num= num ;
ifstream for_output("bank_customer_record.txt",ios::binary);
for_output.seekg(0);


if ( ! for_output) {
cout << " Where is the file ??? Not getting it Bro \n ";
return -1;
}
else {
if ( acct_num == 0){
long position ;
cout <<" Here are the contents of the File";
while(for_output.read((char *) &b2,sizeof(class bank_record))) {
cout << " \n Customer Details";
position=for_output.tellg();
cout << " \n Position is " << position ;
cout << "\n Customer Number :"<< b2->customer_no << endl;
cout << "\n Customer Name :";
puts( b2->customer_name );
cout << endl;
cout << "\n Customer Balance :"<< b2->balance << endl;
}
for_output.close();
return 0;
}
else if ( acct_num == -1) {
while(for_output.read((char *) &b2,sizeof(class bank_record))) {
cout << "\n Customer Number :"<< b2->customer_no << endl;
cout << "\n Customer Name :";
puts( b2->customer_name );
cout << endl;
}
return 1 ;
}
else {
int flag=0;
while(for_output.read((char *) &b2,sizeof(class bank_record))) {
if (b2->customer_no == acct_num ) {
cout << "\n Customer Details";
cout << "\n Customer Account Number :"<< b2->customer_no << endl;
cout << "\n Customer Name :";
puts( b2->customer_name );
cout << endl;
cout << "\n Customer Balance :"<< b2->balance << endl;
for_output.close();
flag=1;
return 0;
}
}
for_output.close();
if ( flag==0) {
cout << " Wrong Account Number Entered \n ";
return -1;
}
}
}

return 0;
}
/*------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------------------------*/

void main () {
clrscr();
int choice,acct_num=0;
char cont_for_more[] ="y";
while ( strcmp(cont_for_more,"n")) {
cout << "Enter you choice 1. Add new account 2. Display All Account 3.Display Specific Account";
cout<< " 4. Modify the Record Deatils";
cin >> choice ;
switch(choice) {

case 1:
bank_record *bank_obj_ptr1=new bank_record;
bank_obj_ptr1->input_record();
bank_obj_ptr1->write_to_file(bank_obj_ptr1);
delete bank_obj_ptr1;
break;
case 2:
bank_record *bank_obj_ptr2=new bank_record;
bank_obj_ptr2->read_from_file(bank_obj_ptr2,0);
delete bank_obj_ptr2;
break;
case 3:
bank_record *bank_obj_ptr3=new bank_record;
if(! ( bank_obj_ptr3->read_from_file(bank_obj_ptr3, -1) == -1)) {
cout<< " Now Enter the account number whose info you want \n ";
cin >> acct_num ;
bank_obj_ptr3->read_from_file(bank_obj_ptr3, acct_num);
}
delete bank_obj_ptr3;
break;
case 4:
bank_record *bank_obj_ptr4=new bank_record;
if(! ( bank_obj_ptr4->read_from_file(bank_obj_ptr4, 0) == -1)) {
cout<< " Now Enter the account number whose info you want to modify \n";
cin >> acct_num ;
bank_obj_ptr4->modify_in_file(bank_obj_ptr4, acct_num);
}
delete bank_obj_ptr4;
break;

}
cout << "\n Want to continue ......:\n";
gets(cont_for_more );
}


}
Topic archived. No new replies allowed.