/*
* 程序的版权和版本声明部分
* Copyright (c)2012, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: fun.cpp
* 作 者:王飞
* 完成日期:2013 年3月 9日
* 版本号: v1.0
* 对任务及求解方法的描述部分:结构体与成绩的文件操作
* 输入描述:略
* 问题描述:略
* 程序输出:如下
*/
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
using namespace std;
void print(); //自定义函数输出结构体的数据
int NUM=0;
struct Student //定义结构体
{
char num[15];
char name[20];
float cpp;
float math;
float english;
};
Student stu[500]; //结构体数组
int m=0;
int main()
{
ifstream infile("数据库.txt",ios::in);
if(!infile) //测试文件
{
cerr<<"open error!"<<endl;
exit(1);
}
while(!infile.eof())
{
infile>>stu[NUM].num>>stu[NUM].name>>stu[NUM].cpp>>stu[NUM].math>>stu[NUM].english; //写入文件
NUM++;
}
NUM-=2;
infile.close();
print();
return 0;
}
void print()
{
while(m<=NUM) //输出结构体
{
cout<<setw(8)<<stu[m].num<<setw(8)<<stu[m].name<<setw(8)<<stu[m].cpp<<setw(8)<<stu[m].math<<setw(8)<<stu[m].english<<endl;
m++;
}
}
输出结果:
心得体会:
只有时刻思索着,才不会忘记!