14.8. Writing Data Randomly to a Random-Access File

Figure 14.12 writes data to the file credit.dat and uses the combination of fstream functions seekp and write to store data at exact locations in the file. Function seekp sets the put file-position pointer to a specific position in the file, then function write outputs the data. Line 6 includes the header ClientData.h defined in Fig. 14.9, so the program can use ClientData objects.


 1   // Fig. 14.12: Fig14_12.cpp
 2   // Writing to a random-access file.
 3   #include <iostream>
 4   #include <fstream>
 5   #include <cstdlib>
 6   #include "ClientData.h" // ClientData class definition
 7   using namespace std;
 8
 9   int main()
10   {
11      int accountNumber;
12      string lastName;
13      string firstName;
14      double balance;
15
16      fstream outCredit( "credit.dat", ios::in | ios::out | ios::binary );
17
18      // exit program if fstream cannot open file
19      if ( !outCredit )
20      {
21         cerr << "File could not be opened." << endl;
22         exit( EXIT_FAILURE );
23      } // end if
24
25      cout << "Enter account number (1 to 100, 0 to end input) ? ";
26
27      // require user to specify account number
28      ClientData client;
29      cin >> accountNumber;
30
31      // user enters information, which is copied into file
32      while ( accountNumber > 0 && accountNumber <= 100 )
33      {
34         // user enters last name, first name and balance
35         cout << "Enter lastname, firstname, balance ? ";
36         cin >> lastName;
37         cin >> firstName;
38         cin >> balance;
39
40         // set record accountNumber, lastName, firstName and balance values
41         client.setAccountNumber( accountNumber );
42         client.setLastName( lastName );
43         client.setFirstName( firstName );
44         client.setBalance( balance );
45
46         // seek position in file of user-specified record   
47         outCredit.seekp( ( client.getAccountNumber() - 1 ) *
48            sizeof( ClientData ) );                          
49
50         // write user-specified information in file                  
51         outCredit.write( reinterpret_cast< const char * >( &client ),
52            sizeof( ClientData ) );                                   
53
54         // enable user to enter another account
55         cout << "Enter account number ? ";
56         cin >> accountNumber;
57      } // end while
58   } // end main


Enter account number (1 to 100, 0 to end input)
? 37
Enter lastname, firstname, balance
? Barker Doug 0.00
Enter account number
? 29
Enter lastname, firstname, balance
? Brown Nancy -24.54
Enter account number
? 96

Enter lastname, firstname, balance
? Stone Sam 34.98
Enter account number
? 88
Enter lastname, firstname, balance
? Smith Dave 258.34
Enter account number
? 33
Enter lastname, firstname, balance
? Dunn Stacey 314.33
Enter account number
? 0


Fig. 14.12. Writing to a random-access file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.85