Writing Data Randomly to a Random-Access File

Figure 17.13 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 write outputs the data. Note that line 19 includes the header file ClientData.h defined in Fig. 17.10, so the program can use ClientData objects.

Figure 17.13. Writing to a random-access file.

(This item is displayed on pages 862 - 864 in the print version)

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

Lines 5960 position the "put" file-position pointer for object outCredit to the byte location calculated by

( client.getAccountNumber() - 1 ) * sizeof( ClientData )

Because the account number is between 1 and 100, 1 is subtracted from the account number when calculating the byte location of the record. Thus, for record 1, the file-position pointer is set to byte 0 of the file. Note that line 28 uses the fstream object outCredit to open the existing credit.dat file. The file is opened for input and output in binary mode by combining the file-open modes ios::in, ios::out and ios::binary. Multiple fileopen modes are combined by separating each open mode from the next with the bitwise inclusive OR operator (|). Opening the existing credit.dat file in this manner ensures that this program can manipulate the records written to the file by the program of Fig. 17.12, rather than creating the file from scratch. Chapter 22, Bits, Characters, Strings and structs, discusses the bitwise inclusive OR operator in detail.

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net