943,884 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1143
  • C++ RSS
Dec 5th, 2008
0

Random access files problem

Expand Post »
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)
  1. fstream customerFile("customer.txt", ios::in | ios::out | ios::binary);
  2.  
  3. void createCustomerFile( fstream & customerFile )
  4. {
  5. int MAXREC;
  6. cout << "Enter number of records you want to create for customer File" << endl;
  7. cin >> MAXREC;
  8.  
  9. C blankRec = { 0 }; // blank record to use to create file
  10.  
  11.  
  12.  
  13. for( int k = 0; k < MAXREC; k++ )
  14. customerFile.write(reinterpret_cast <char *> ( &blankRec), sizeof( C ));
  15.  
  16. }
  17.  
  18. void newCustomer( fstream & customerFile, int latestCustomerID, int acct, int transactionNum, int accNumber)
  19. {
  20. C record;
  21.  
  22. //move to start of record
  23. customerFile.seekg((acct ) * sizeof( C ), ios::beg );
  24. customerFile.read( reinterpret_cast<char *> (&record), sizeof( C ) );
  25.  
  26. if ( record.customerID == 0 )
  27. cout << "Customer account is there already.\n";
  28. else
  29. {
  30. record.customerID = latestCustomerID;
  31. cin.get();
  32. cout << "Enter first name "<<endl;
  33. gets(record.name);
  34.  
  35. cout << "Enter surname" << endl;
  36. gets(record.surname);
  37.  
  38. cout << "Enter number of Accounts" << endl;
  39. cin >> record.numAccounts;
  40.  
  41. record.accArray = new A[record.numAccounts]; //create dynamic array
  42.  
  43. for(int i =0; i < record.numAccounts;i++)
  44. {
  45. record.accArray[i].accNumber = accNumber;
  46.  
  47. record.accArray[i].transactionID = transactionNum;
  48. cout << "Transaction ID = " << record.accArray[i].transactionID << endl;
  49.  
  50.  
  51. cout << "Enter account type, either D or C" << endl;
  52. cin >> record.accArray[i].accType;
  53.  
  54. if(record.accArray[i].accType == 'D' || record.accArray[i].accType == 'd' )
  55. {
  56. cout <<"Enter intrest Rate" << endl;
  57. cin >> record.accArray[i].intrestRate;
  58.  
  59. cout <<"Enter balance" << endl;
  60. cin >> record.accArray[i].balance;
  61. cout << "acc Number " << record.accArray[i].accNumber <<endl;
  62. accNumber++;
  63.  
  64. }
  65. else if(record.accArray[i].accType == 'C' || record.accArray[i].accType == 'c')
  66. {
  67.  
  68. cout <<"Enter balance" << endl;
  69. cin >> record.accArray[i].balance;
  70. cout << "acc Number " << record.accArray[i].accNumber <<endl;
  71. accNumber++;
  72. }
  73. else if(record.accArray[i].accType != 'c' || 'd')
  74. {
  75. cout << "Error you must redo this step, you have not created an account" << endl;
  76. }
  77.  
  78.  
  79. }
  80.  
  81.  
  82. customerFile.seekp( ( acct ) * sizeof( C ), ios::beg );
  83. customerFile.write( reinterpret_cast< char* > (&record), sizeof( C ) );
  84. customerID++;
  85. infile.close();
  86. }
  87.  
  88. return;
  89.  
  90.  
  91. }
  92.  
  93. void updateCustomer(fstream & customerFile)
  94. {
  95. C record;
  96. int customerid;
  97.  
  98. cout << "Enter customer id" << endl;
  99. cin >> customerid;
  100.  
  101. customerFile.seekg((customerid) * sizeof( C ), ios::beg );
  102. customerFile.read(reinterpret_cast<char*>(&record),sizeof(C));
  103.  
  104. int numBytes = customerFile.tellg();
  105. int offset = (numBytes - 1) * sizeof(C);
  106. customerFile.seekp(offset - sizeof(C));
  107.  
  108. char option;
  109. bool menuDisplay = true;
  110. while(menuDisplay == true)
  111. {
  112.  
  113. system("CLS");
  114. cout << MENU2;
  115. cin >> option;
  116. option = toupper(option);
  117. cin.get();
  118.  
  119. if(option == 'n')
  120. {
  121. cin.get();
  122. gets(record.name);
  123. cout << "changing the first name" << endl;
  124. system("PAUSE");
  125.  
  126. }
  127. else if(option == 's')
  128. {
  129. cin.get();
  130. gets(record.surname);
  131. cout << "changing the surname" << endl;
  132. system("PAUSE");
  133.  
  134. }
  135. else if(option == 'X')
  136. {
  137.  
  138. menuDisplay = false;
  139.  
  140. }
  141. }
  142.  
  143. cout << record.name;
  144. cout << record.surname;
  145.  
  146. customerFile.write( reinterpret_cast<char *> (&record), sizeof( C ) );
  147.  
  148. }
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008
Dec 5th, 2008
0

Re: Random access files problem

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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Dec 5th, 2008
0

Re: Random access files problem

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ku.bis is offline Offline
2 posts
since Dec 2008
Dec 5th, 2008
0

Re: Random access files problem

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 - 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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ku.bis is offline Offline
2 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Still having problem with random!!
Next Thread in C++ Forum Timeline: template of template of base





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC