c語言讀取順序文件并處理
我們今天學習如何在創(chuàng)建讀取文件之后,對其進行處理!不妨看看c語言如何讀取順序文件并處理,以下僅供參考!
以下是代碼:
# include
# include
# include
# include
# include
using namespace std;
enum requesttype{ZERO_BALANCE=1,CREDIT_BALANCE, DEBIT_BANLANCE,END};/pic/p>
int getrequest();
bool shoulddisplay(int, double);/pic/p>
void outputline(int, const string, double);/pic/p>
int main() {
ifstream inclientfile("clients.dat", ios::in);/pic/p>
if (!inclientfile) {
cerr << "file could not be opened" << endl;
exit(1);
}
int request;
int account;
char name[30];
double balance;
request = getrequest();
while (request != END) {/pic/p>
switch (request) {
case ZERO_BALANCE:
cout << " accounts with zero balances: ";
break;
case CREDIT_BALANCE:
cout << " accounts with creadit balances: ";
break;
case DEBIT_BANLANCE:
cout << " accounts with debit balances: ";
break;
}
inclientfile >> account >> name >> balance;/pic/p>
while (!inclientfile.eof()) {/pic/p>
if (shoulddisplay(request, balance)) {
outputline(account, name, balance);
}
inclientfile >> account >> name >> balance;
}
inclientfile.clear();
inclientfile.seekg(0);/pic/p>
request = getrequest();
}
cout << "end of run." << endl;
system("pause");
return 0;
}
int getrequest() {
int request;
cout << " enter request" << endl
<< "1-list accounts with zero balances" << endl
<< "2-list accounts with credit balances" << endl
<< "3-list accounts with debit balances" << endl
<< "4-end of run" << fixed << showpoint;
do {
cout << " ?";
cin >> request;
} while (requestEND);
return request;
}
bool shoulddisplay(int type, double balance) {
if (type == ZERO_BALANCE&&balance == 0) {
return true;
}
if (type == CREDIT_BALANCE&&balance < 0) {
return true;
}
if (type == DEBIT_BANLANCE&&balance > 0) {
return true;
}
return false;
}
void outputline(int account, const string name, double balance) {
cout << left << setw(10) << account << setw(13) << name
<< setw(7) << setprecision(2) << right << balance << endl;
}
以下是執(zhí)行后結果:
【c語言讀取順序文件并處理】相關文章:
C語言文件03-02
C語言順序結構10-07
C語言預處理概述以及文件包含命令11-15
C語言讀取word文檔的方法10-31
C語言的文件概念10-20
C語言順序存儲結構01-17
C語言文件的創(chuàng)建與建立02-14
C語言頭文件封裝03-18
c語言文件創(chuàng)建與建立01-13