C
C
C++ I/O streams. Reading and writing sequential files. Reading and writing random access files.
Sequential file
cout << "Enter the account, name, and balance.\n" << "Enter end-of-file to end input.\n? "; int account; char name[ 30 ]; float balance;
while ( cin >> account >> name >> balance ) { outClientFile << account << ' ' << name << ' ' << balance << '\n'; cout << "? "; }
return 0; // ofstream destructor closes file }
CPSC 231 D.H. C++ File Processing 4
Question.
What does the above program do?
OR
Ofstream outClientFile; outClientFile.open(clients.dat, ios:out)
10
int account; char name[ 30 ]; double balance; cout << setiosflags( ios::left ) << setw( 10 ) << "Account" << setw( 13 ) << "Name" << "Balance\n"; while ( inClientFile >> account >> name >> balance ) outputLine( account, name, balance ); return 0; // ifstream destructor closes the file } void outputLine( int acct, const char *name, double bal ) { cout << setiosflags( ios::left ) << setw( 10 ) << acct << setw( 13 ) << name << setw( 7 ) << setprecision( 2 ) << resetiosflags( ios::left ) << setiosflags( ios::fixed | ios::showpoint ) << bal << '\n'; }
CPSC 231 D.H. C++ File Processing 11
12
17
for ( int i = 0; i < 100; i++ ) outCredit.write (reinterpret_cast<const char *>( &blankClient ), sizeof( clientData ) ); return 0; }
20
The write function expects a first argument of type const char *, hence we used the reinterpret_cast <const char *> to convert the address of the blankClient to a const char *. The second argument of write is an integer of type size_t specifying the number of bytes to written. Thus the sizeof( clientData ).
CPSC 231 D.H. C++ File Processing 22
23
cout << "Enter account number " << "(1 to 100, 0 to end input)\n? "; clientData client; cin >> client.accountNumber;
while ( client.accountNumber > 0 && client.accountNumber <= 100 ) { cout << "Enter lastname, firstname, balance\n? "; cin >> client.lastName >> client.firstName >> client.balance;
24
outCredit.seekp( ( client.accountNumber - 1 ) * sizeof( clientData ) ); outCredit.write( reinterpret_cast<const char *>( &client ), sizeof( clientData ) );
return 0;
}
25
cout << setiosflags( ios::left ) << setw( 10 ) << "Account" << setw( 16 ) << "Last Name" << setw( 11 ) << "First Name" << resetiosflags( ios::left ) << setw( 10 ) << "Balance" << endl;
clientData client;
inCredit.read( reinterpret_cast<char *>( &client ), sizeof( clientData ) );
27
while ( inCredit && !inCredit.eof() ) { if ( client.accountNumber != 0 ) outputLine( cout, client ); inCredit.read( reinterpret_cast<char *>( &client ), sizeof( clientData ) );
}
return 0; }
28
void outputLine( ostream &output, const clientData &c ) { output << setiosflags( ios::left ) << setw( 10 ) << c.accountNumber << setw( 16 ) << c.lastName << setw( 11 ) << c.firstName << setw( 10 ) << setprecision( 2 ) << resetiosflags( ios::left ) << setiosflags( ios::fixed | ios::showpoint ) << c.balance << '\n'; }
29
The <istream> function inputs a specified (by sizeof(clientData)) number of bytes from the current position of the specified stream into an object.
CPSC 231 D.H. C++ File Processing 30