// fig .14.15 :fig14_15.cpp
// this program reads a random access file sequentially,
// updates data already written to the file ,creates new
// data to be placed in the file, and deletes data
// already in the file.
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include "clntdata.h"
int enterchoice();
void textFile( fstream & );
void updateRecord( fstream & );
void newRecord( fstream & );
void deleteRecord( fstream & );
void outPutLine( ostream &, const clientData & );
int getAccount( const char * );
enum Choices { TEXTFILE = 1, UPDATE, NEW, DELETE, END };
int main()
{
fstream inOutCredit;
inOutCredit.open( "credit.dat", ios::in | ios::out );
if( !inOutCredit ) {
cerr<< " File could not be opened"<< endl;
exit(1);
}
// 初始化100个帐户
clientData blankclient = { 0,"","",0.0 };
for( int i=0;i<100;i++) {
inOutCredit.write( reinterpret_cast<char *>( &blankclient ),sizeof( clientData) );
// reinterpret_cast<type *>(exp) 用于任何类弄的转换
// write / read 用于随机文件写/读
}
int choice;
while( ( choice = enterchoice() ) != END ) {
switch ( choice ) {
case TEXTFILE:
textFile( inOutCredit );
break;
case UPDATE:
updateRecord( inOutCredit );
break;
case NEW:
newRecord( inOutCredit );
break;
case DELETE:
deleteRecord( inOutCredit );
break;
default:
cerr<< " Incorrect choice\n";
break;
}
inOutCredit.clear();
}
return 0;
}
// 输入选择
int enterchoice()
{
cout<< "\nEnter your choice"<< endl
<< "1 - store a formatted text file of accounts\n"
<< " called \"print.txt\" for printing\n"
<< "2 - update an account\n"
<< "3 - add a new account\n"
<< "4 - delete an account\n"
<< "5 - end program\n>> ";
int menuchoice;
cin>> menuchoice;
return menuchoice;
}
// 保存为txt
void textFile( fstream &readFromFile )
{
ofstream outPrintFile;
outPrintFile.open( "print.txt", ios::out );
if( ! outPrintFile ) {
cerr<< "File could not be opened."<< endl;
exit(1);
}
outPrintFile << setiosflags( ios::left ) << setw(10)
<< "Account" << setw(16) << "Last Name" << setw(11)
<< "First Name" << resetiosflags( ios::left )
<< setw(10) << "Balance"<< endl;
readFromFile.seekg( 0 );
clientData client;
//读出 一条记录
readFromFile.read( reinterpret_cast<char *>( &client ),sizeof( clientData) );
// 写入txt
while( ! readFromFile.eof() ) {
if( client.accountNumber != 0 )
outPutLine( outPrintFile, client ); // 写入
//读下一条记录
readFromFile.read( reinterpret_cast<char *>( &client ),sizeof( clientData) );
}
}
// update an account's balance
void updateRecord( fstream &updateFile )
{
int account = getAccount( "Enter account to update" );
updateFile.seekg( ( account -1 ) * sizeof( clientData ) );
clientData client;
updateFile.read( reinterpret_cast<char *>( &client ),sizeof( clientData) );
if( client.accountNumber != 0) {
outPutLine( cout, client );
cout<< "\nEnter charge(+) or payment (-):";
float transaction;
cin>> transaction;
client.balance += transaction;
outPutLine( cout, client );
updateFile.seekg( ( account -1 ) * sizeof( clientData ) );
updateFile.write( reinterpret_cast<char *>( &client ),sizeof( clientData) );
}
else
cerr<< "Account #"<< account
<< " has no information." << endl;
}
// create and insert new record
void newRecord( fstream &insertInFile )
{
int account = getAccount( "Enter new account number");
insertInFile.seekg( ( account -1 ) * sizeof( clientData ) );
clientData client;
insertInFile.read( reinterpret_cast<char *>( &client ),sizeof( clientData) );
if( client.accountNumber == 0 ) {
cout<< "Enter lastName, firstName, balance\n? ";
cin>> client.lastName >> client.firstName >> client.balance;
client.accountNumber = account;
insertInFile.seekg( ( account -1 ) * sizeof( clientData ) );
insertInFile.write( reinterpret_cast<char *>( &client ),sizeof( clientData) );
}
else
cerr << "Account #"<< account
<< " already contains information." << endl;
}
// delete
void deleteRecord( fstream &deleteFormFile )
{
int account = getAccount( "Enter account to delete" );
deleteFormFile.seekg( ( account -1 ) * sizeof( clientData ) );
clientData client;
deleteFormFile.read( reinterpret_cast<char *>( &client ),sizeof( clientData) );
if( client.accountNumber == 0 ) {
clientData blankClient = { 0,"","",0.0 };
deleteFormFile.read( reinterpret_cast<char *>( &client ),sizeof( clientData) );
cout<< " Acount #"<< account << " deleted." << endl;
}
else
cerr<< " Acount #"<< account << " is empty." << endl;
}
void outPutLine ( ostream &output, const clientData &c )
{
output << setiosflags( ios::left ) << setw(10)
<< c.accountNumber << setw(16) << c.lastName << setw(11)
<< c.firstName << resetiosflags( ios::left )
<< setw(10) << c.balance << endl;
}
int getAccount ( const char *prompt )
{
int account;
do {
cout<< prompt << " (1-100) : ";
cin>>account;
}
while( account <1 || account >100 );
return account;
}
#ifndef CLNTDATA_H
#define CLNTDATA_H
struct clientData{
int accountNumber;
char lastName[15];
char firstName[10];
float balance;
};
#endif
转载于:https://my.oschina.net/delmore/blog/4738