C語言

當前位置 /首頁/計算機/C語言/列表

c語言—檔案的建立與建立

今天要介紹的是有關檔案的建立與讀取的語法,事實上,c語言中對於這方面的`已經有相當經典且應用相當廣泛的語法了,但是我今天想講一講關於c++中的相關語法,以下僅供參考!

c語言—檔案的建立與建立

以下是程式碼:

首先是檔案的建立:

# include

# include

# include

using namespace std;

int main() {

ofstream outclientfile("", 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("", 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<<fixed<<showpoint;

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;

}

TAG標籤:檔案 建立 語言 #