- 相關推薦
C語言文件的創建與建立
c語言中對于文件的創建與建立已經有相當經典且應用相當廣泛的語法了。下面是小編為大家帶來的C語言文件的創建與建立的知識,歡迎閱讀。
首先是文件的創建:
# include
# include
# include
using namespace std;
int main() {
ofstream outclientfile("clients.dat", ios::out);
if (!outclientfile) {
cerr << "file could not be opend" << endl;
exit(1);
}
cout << "enter the account,name,and balance." << endl;
cout<< "enter end-of-file to end input. ?";
int account;
char name[30];
double balance;
while (cin >> account >> name >> balance) {
outclientfile << account << " " << name << " " << balance << endl;
cout << "?";
}
system("pause");
return 0;
}
以下是文件的讀取:
# include
# include
# include
# include
# include
using namespace std;
void outputline(int, const string, double);
int main() {
ifstream inclientfile("clients.dat", ios::in);
if (!inclientfile) {
cerr << "file could not be opened" << endl;
exit(1);
}
int account;
char name[30];
double balance;
cout << left << setw(10) << "account" << setw(13) << "name"
<< "balance" << endl<
while (inclientfile >> account >> name >> balance) {
outputline(account, name, balance);
}
system("pause");
return 0;
}
void outputline(int account, const string name, double balance) {
cout << left << setw(10) << account << setw(13) << name
<< setw(7) << setprecision(2) << right << balance << endl;
}
知識點:以文件的創建為例,我們在頭文件中使用# include包含了ofstream類,并且在主程序中使用類ofstream建立了名為outclientfile對象,并且初始化其構造函數。要注意的是我們在while只是判斷條件的真假,而類outclientfile進行輸入數據,在這里我也有疑問的是?在編譯為什么是出現在輸入數據之前的?這一點以后明白了再找機會說明,或者有知道的小伙伴也可以發消息告知我一下?
【C語言文件的創建與建立】相關文章:
怎么利用c語言創建excel文件12-04
C語言頭文件封裝11-27
C語言項目中.h文件和.c文件的關系詳解05-08
C語言頭文件避免重復包含的方法技巧03-27
C語言以數據塊的形式讀寫文件實例代碼12-04
2023計算機二級C語言文件知識點04-15
C語言考點精選03-18
C語言試題03-28
C語言的應用12-12