日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

随机文件处理函数应用

發布時間:2025/3/19 编程问答 11 豆豆
生活随笔 收集整理的這篇文章主要介紹了 随机文件处理函数应用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

// 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) );// 寫入txtwhile( ! 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) );}elsecerr<< "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) );}elsecerr << "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;}elsecerr<< " 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_Hstruct clientData{int accountNumber;char lastName[15];char firstName[10];float balance; };#endif

轉載于:https://my.oschina.net/delmore/blog/4738

總結

以上是生活随笔為你收集整理的随机文件处理函数应用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。