荟萃馆

位置:首页 > 计算机 > C语言

c语言—文件的创建与建立

C语言3.01W

今天要介绍的是有关文件的创建与读取的语法,事实上,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;

}

标签:语言 文件 建立