首页 > 互联网 > 计算机 > C语言 > C语言文件的创建与建立

C语言文件的创建与建立

   来源:学问社    阅读: 1.81W 次
字号:

用手机扫描二维码 在手机上继续观看

手机查看

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<

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语言
java语言
php语言
网络技术