Hey can anyone offer me any advice on updating my customer file, I created the blank records and can create a record in the newCustomer function but can’t seem to access that record again for updating, does anyone know where I’m gone wrong. Any help would be greatly appreciated.

fstream  customerFile("customer.txt", ios::in | ios::out | ios::binary);

void createCustomerFile( fstream & customerFile )
{
   int MAXREC;
   cout << "Enter number of records you want to create for customer File" << endl;
   cin  >> MAXREC;
   
   C  blankRec = { 0 };      // blank record to use to create file
   
   
      
   for( int k = 0; k < MAXREC; k++ )
       customerFile.write(reinterpret_cast <char *> ( &blankRec), sizeof( C ));
      
 }

void newCustomer( fstream & customerFile, int latestCustomerID, int acct, int transactionNum, int accNumber)
{
   C record;  
    
    //move to start of record
   customerFile.seekg((acct ) * sizeof( C ), ios::beg );
   customerFile.read( reinterpret_cast<char  *> (&record), sizeof( C ) );

   if ( record.customerID == 0 )
      cout <<  "Customer  account is  there already.\n";
   else
   {
       record.customerID = latestCustomerID;
      	cin.get();
      	cout << "Enter first name "<<endl;
        gets(record.name);
        
        cout << "Enter surname" << endl;
        gets(record.surname);
		
        cout << "Enter number of Accounts" << endl;
        cin  >> record.numAccounts;
        
        record.accArray = new A[record.numAccounts];    //create dynamic array
                    
		for(int i =0; i < record.numAccounts;i++)
        {
        	record.accArray[i].accNumber = accNumber;
        	
            record.accArray[i].transactionID = transactionNum; 
            cout << "Transaction ID = " << record.accArray[i].transactionID << endl;
        
            
            cout << "Enter account type, either D or C" << endl;
            cin  >> record.accArray[i].accType;
            
            if(record.accArray[i].accType == 'D' || record.accArray[i].accType == 'd' )
            {
            cout <<"Enter intrest Rate" << endl;
            cin  >> record.accArray[i].intrestRate;
            
            cout <<"Enter balance" << endl;
            cin  >> record.accArray[i].balance;
			cout << "acc Number " << record.accArray[i].accNumber <<endl;
			accNumber++;
			
            }
            else if(record.accArray[i].accType == 'C' || record.accArray[i].accType == 'c')
            {

            cout <<"Enter balance" << endl;
            cin  >> record.accArray[i].balance;
            cout << "acc Number " << record.accArray[i].accNumber <<endl;
 			accNumber++;
            }
            else if(record.accArray[i].accType != 'c' || 'd') 
            {
            cout << "Error you must redo this step, you have not created an account" << endl;
            }
            
        
        }
     
       
        customerFile.seekp( ( acct ) * sizeof( C ), ios::beg );
        customerFile.write( reinterpret_cast< char*  > (&record), sizeof( C ) );
        customerID++;
  		infile.close();
   }
    
    return;
    
     
}

void updateCustomer(fstream & customerFile)
{
	C record;
	int customerid;
	
	cout << "Enter customer id" << endl;
	cin  >> customerid;
		
	customerFile.seekg((customerid) * sizeof( C ), ios::beg );
	customerFile.read(reinterpret_cast<char*>(&record),sizeof(C));
	
	int numBytes = customerFile.tellg();
	int offset = (numBytes - 1) * sizeof(C);
	customerFile.seekp(offset - sizeof(C));
				
char option;
bool menuDisplay = true;				
while(menuDisplay == true)
{	

    system("CLS");
	cout << MENU2;
	cin  >> option;
	option = toupper(option);
	cin.get();
	
    if(option == 'n')
	{
	cin.get();
	gets(record.name);
	cout << "changing the first name" << endl;
	system("PAUSE");

	}
	else if(option == 's')
	{
	cin.get();
	gets(record.surname);
	cout << "changing the surname" << endl;
	system("PAUSE");

	}
	else if(option == 'X')
	{
	
	menuDisplay = false;
	
	}
}
	
	cout << record.name;
	cout << record.surname;
				
	customerFile.write( reinterpret_cast<char  *> (&record), sizeof( C ) );	

}

When is the problem, unexpected out put wtc. We cannot judge what you have done incorrectly if you don't tell us how it is wrong

Chris

It looks exactly like my homework. I guess your teacher is Gemma;)
I am solving similar problem. But I at least know my problem is in the line with file.read(reinterpret_cast<char*> (&record), sizeof(C)) . It just can't pass any data it read into the record variable. Instead of zeros is record variable full of rubish data and I don't know how to solve it. It seems like this could be your problem too, because the rubish in record.customerID was by the chance 0 so it allowes you to "write" the data into file (actually doesn't write-it only doesn't give you any error), but later it doesn't allow you to update it because you have in record.customerID still 0.
The other problem I see is dynamic account array which can make the record having a variable length and that is not good for binary files. You should have a fixed number of accounts there instead.

I would really appreciate if someone could help me with the problem I described above. If it isn't clear enough I try to describe again as my English is only second language and sometimes not understandable.

Thanks in advance
Kuba

Yeah, I have my problem solved - I left the seekg at the end of the file before going to read it again.
I see you have there more problems. I told you, if you want to acces the files in random, all records have to be the same length -> fixed number of accounts in customer struct.
Another problem in you updateCustomer() code is you're moving into incorrect possition inside the file - you should have customerFile.seekg((customerid[B] - 1[/B]) * sizeof( C ), ios::beg ) . Note the - 1 inside the function.

I didn't check it into every detail, so I don't know if there's more troubles waiting for you.

Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.