- 论坛徽章:
- 0
|
#include<stdio.h>
#include<string.h>
#define SIZE 10
struct student_type
{
int num;
char name[10];
int age;
char sex;
float score[5];
}std1[SIZE],std2[SIZE];
void input_data()
{
FILE *fp;
int i;
char infile[10];
printf("Enter the infilename:\n");
scanf("%s",infile);
if ((fp = fopen(infile,"rb")) == NULL)
{
printf("cannot open infile\n");
return;
}
for (i = 0; i < SIZE; i++)
{
if (fread(&std1[i],sizeof(struct student_type),1,fp) != 1)
{ if(feof(fp))
{fclose(fp);return;}
printf("file read error\n");
}
}
for (i = 0; i < SIZE; i++)
printf("%4d %-10s %4d %5s %4d %4d %4d %4d %4d\n",
std1[i].num,std1[i].name,std1[i].age,std1[i].sex,
std1[i].score[1],std1[i].score[2],
std1[i].score[3],std1[i].score[4],std1[i].score[5]);
fclose (fp);
}
void output_data()
{
int i;
input_data();
printf("num name age sex computer progrom PE english maths\n");
for (i = 0; i < SIZE; i++)
printf("%4d %-10s %4d %5s %4d %4d %4d %4d %4d\n",
std1[i].num,std1[i].name,std1[i].age,std1[i].sex,
std1[i].score[1],std1[i].score[2],
std1[i].score[3],std1[i].score[4],std1[i].score[5]);
}
void data_write()
{
FILE *out;
char outfile[10];
int i;
input_data();
printf("Enter the outfile name:\n");
scanf("%s",outfile);
out = fopen(outfile,"rb");
for (i = 0; i < SIZE; i++)
if (fwrite(&std1[i],sizeof(struct student_type),1,out) != 1)
printf("file write error\n");
fclose(out);
}
void main()
{
char ch;
ch = getchar();
switch(ch)
{ case '1':input_data();break;
case '2':output_data();break;
case '3':data_write();break;
}
}
此函数是为了实现:
设计一个函数,实现将10个学生的数据信息读入到数组std1[10]中。(input_data( )函数)
设计一个函数,实现将10个学生的数据信息输出到屏幕中。(output_data( )函数)
定义一个函数,功能是将数组std1[10]中的数据写入一个名为std.dat的二进制文件中。(data_write( )函数)
但结果难于实现,小弟是莱鸟!!!
向各位高手请教了! |
|