| | |
Random access files problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 34
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
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 ) ); }
•
•
Join Date: Dec 2008
Posts: 2
Reputation:
Solved Threads: 0
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
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

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
•
•
Join Date: Dec 2008
Posts: 2
Reputation:
Solved Threads: 0
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
I didn't check it into every detail, so I don't know if there's more troubles waiting for you.
Good luck!
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 - 1) * 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!
![]() |
Similar Threads
- fstream Tutorial (C++)
- Forms in Random access files (Visual Basic 4 / 5 / 6)
- VB 6.0. Creating a file for random access (Visual Basic 4 / 5 / 6)
- traversing files and skipping lines (C)
- Help needed with VB Assignment (Visual Basic 4 / 5 / 6)
- pls help me (Visual Basic 4 / 5 / 6)
- file processing, Random-access Files (C++)
- Intermitent access to google and other sites (Viruses, Spyware and other Nasties)
- Random Reboots when Booting From a CD (Troubleshooting Dead Machines)
- Random popups, even when not online (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Still having problem with random!!
- Next Thread: template of template of base
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





