- 相關(guān)推薦
c語言讀取順序文件并處理
我們今天學習如何在創(chuàng)建讀取文件之后,對其進行處理!不妨看看c語言如何讀取順序文件并處理,以下僅供參考!
以下是代碼:
# include
# include
# include
# include
# include
using namespace std;
enum requesttype{ZERO_BALANCE=1,CREDIT_BALANCE, DEBIT_BANLANCE,END};//這里定義的是枚舉類型,也就是賦值1,2,3
int getrequest();
bool shoulddisplay(int, double);//這個函數(shù)的作用,就是從讀取的數(shù)據(jù)中選擇不同的條件進行輸出!
void outputline(int, const string, double);//輸出數(shù)據(jù)
int main() {
ifstream inclientfile("clients.dat", ios::in);//我們假定已經(jīng)定義好了相關(guān)的數(shù)據(jù)在文件clients.dat中!
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) {//選擇數(shù)據(jù)類型
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;//讀入數(shù)據(jù)
while (!inclientfile.eof()) {//設(shè)置循環(huán)條件
if (shoulddisplay(request, balance)) {
outputline(account, name, balance);
}
inclientfile >> account >> name >> balance;
}
inclientfile.clear();
inclientfile.seekg(0);//回到文件的起始位置
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í)行后結(jié)果:
【c語言讀取順序文件并處理】相關(guān)文章:
C語言文件08-28
C語言順序結(jié)構(gòu)07-10
C語言讀取word文檔的方法08-21
C語言預(yù)處理概述以及文件包含命令08-26
C語言的文件概念07-18
C語言順序存儲結(jié)構(gòu)07-10
c語言文件創(chuàng)建與建立05-31
C語言頭文件封裝06-25
C語言文件的創(chuàng)建與建立08-12