|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么读取失败。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAXSTU 200 /*添加学生的最大人数*/
struct student
{
long num;
char name[20];
float math, English, C, physics;
float sum;
float average;
}msg[MAXSTU];
typedef struct student * pst; /* 定义指针类型 */
int count = 0;
int menu_select() // 选择功能菜单;
{
int i;
printf(" 1.添加学生信息\n");
printf(" 2.显示学生信息\n");
printf(" 3.保存学生信息\n");
printf(" 4.读取学生信息\n");
printf(" 5.退出程序 \n");
do
{
printf("请输入功能序号: ");
scanf("%d", &i);
}while(i<=0||i>7);
return i;
}
void add_student(pst p); //添加学生成绩
void dis_student(pst p); //显示学生成绩
void save_student(); //保存学生信息
void read_student(); // 读取学生信息
int main()
{
pst p = msg; /* 定义指针 */
while(1)
{
system("cls");
switch(menu_select())
{
case 1:
{
add_student(p);
break;
}
case 2:
{
dis_student(p);
break;
}
case 3:
{
save_student();
break;
}
case 4:
{
read_student();
break;
}
case 5:
exit(0);
}
}
system("pause");
return 0;
}
void add_student(pst p)
{
float temp;
printf("\t (学号为零输入结束)\n");
do
{
printf("请输入学号:");
scanf("%ld", &(p+count)->num);
if ((p+count)->num == 0) /* 输入学号为零,退出循环 */
break;
printf("请输入学生的名字: ");
scanf("%s",&(p+count)->name);
printf("请输入高数的成绩: ");
scanf("%f", &temp);
(p+count)->math = temp;
printf("请输入英语的成绩: ");
scanf("%f", &temp);
(p+count)->English = temp;
printf("请输入C语言的成绩: ");
scanf("%f", &temp);
(p+count)->C = temp;
printf("请输入物理的成绩: ");
scanf("%f", &temp);
(p+count)->physics = temp;
(p+count)->sum = ((p+count)->math + (p+count)->English +
(p+count)->C + (p+count)->physics);
(p+count)->average = ((p+count)->sum) / 4;
count++;
printf("输入完成,请按任意键继续.....\n");
getch();
} while (1);
}
void dis_student(pst p) //显示学生成绩
{
int i;
if(count)
{
printf("===========================================================================\n");
printf(" \t学号\t姓名\t高数\t英语\tC语言\t物理\t总分\t平均分\n");
for (i = 0; i < count; i++)
{
printf(" %d\t%ld\t%s\t%0.1f\t%0.1f\t%0.1f\t%0.1f\t%0.1f\t%0.1f\n\n", (p+i)->num, (p+i)->name,
(p+i)->math, (p+i)->English, (p+i)->C, (p+i)->physics, (p+i)->sum, (p+i)->average);
}
}
else
printf("当前无学生信息.......\n");
system("pause");
}
void save_student() // 保存学生成绩
{
int i;
printf("\n\t\t\t********保存学生信息********\t\t\t\n");
FILE *fp=fopen("student.txt","wb");
for(i=0;i<count;i++)
{
if(fwrite(&msg[i],sizeof(struct student),1,fp)!=1)
printf("\t error \n");
}
fclose(fp);
system("pause");
}
void read_student() //读取学生信息
{
int i,f;
FILE *fp;
printf("\n\t\t\t*******读取学生信息*******\t\t\t\n");
if((fp = fopen("student.txt","rb")) == NULL)
{
printf("Can not open file\n");
if((fp=fopen("student.txt","w"))==NULL)
printf("文件无法打开");
}
fseek(fp,0,SEEK_END);
if(ftell(fp)>0)
{
rewind(fp);
for(i=0;i<count;i++)
{
f=fread(&msg[i],sizeof(struct student),1,fp);
}
}
if(f!=1)
{
printf("文件导入失败.");
}
else
printf("文件导入成功");
system("pause");
fclose(fp);
}
参考: #include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAXSTU 200 /*添加学生的最大人数*/
struct student
{
long num;
char name[20];
float math, English, C, physics;
float sum;
float average;
}msg[MAXSTU];
typedef struct student * pst; /* 定义指针类型 */
int count = 0;
int menu_select() // 选择功能菜单;
{
int i;
printf(" 1.添加学生信息\n");
printf(" 2.显示学生信息\n");
printf(" 3.保存学生信息\n");
printf(" 4.读取学生信息\n");
printf(" 5.退出程序 \n");
do
{
printf("请输入功能序号: ");
scanf("%d", &i);
}while(i<=0||i>7);
return i;
}
void add_student(pst p); //添加学生成绩
void dis_student(pst p); //显示学生成绩
void save_student(); //保存学生信息
void read_student(); // 读取学生信息
int main()
{
pst p = msg; /* 定义指针 */
while(1)
{
system("cls");
switch(menu_select())
{
case 1:
{
add_student(p);
break;
}
case 2:
{
dis_student(p);
break;
}
case 3:
{
save_student();
break;
}
case 4:
{
read_student();
break;
}
case 5:
exit(0);
}
}
system("pause");
return 0;
}
void add_student(pst p)
{
float temp;
printf("\t (学号为零输入结束)\n");
do
{
printf("请输入学号:");
scanf("%ld", &(p+count)->num);
if ((p+count)->num == 0) /* 输入学号为零,退出循环 */
break;
printf("请输入学生的名字: ");
scanf("%s",&(p+count)->name);
printf("请输入高数的成绩: ");
scanf("%f", &temp);
(p+count)->math = temp;
printf("请输入英语的成绩: ");
scanf("%f", &temp);
(p+count)->English = temp;
printf("请输入C语言的成绩: ");
scanf("%f", &temp);
(p+count)->C = temp;
printf("请输入物理的成绩: ");
scanf("%f", &temp);
(p+count)->physics = temp;
(p+count)->sum = ((p+count)->math + (p+count)->English +
(p+count)->C + (p+count)->physics);
(p+count)->average = ((p+count)->sum) / 4;
count++;
printf("输入完成,请按任意键继续.....\n");
getch();
} while (1);
}
void dis_student(pst p) //显示学生成绩
{
int i;
if(count)
{
printf("===========================================================================\n");
printf(" \t学号\t姓名\t高数\t英语\tC语言\t物理\t总分\t平均分\n");
for (i = 0; i < count; i++)
{
printf(" %d\t%ld\t%s\t%0.1f\t%0.1f\t%0.1f\t%0.1f\t%0.1f\t%0.1f\n\n", (p+i)->num, (p+i)->name,
(p+i)->math, (p+i)->English, (p+i)->C, (p+i)->physics, (p+i)->sum, (p+i)->average);
}
}
else
printf("当前无学生信息.......\n");
system("pause");
}
void save_student() // 保存学生成绩
{
int i;
FILE *fp=fopen("student.txt","wb");
printf("\n\t\t\t********保存学生信息********\t\t\t\n");
for(i=0;i<count;i++)
{
if(fwrite(&msg[i],sizeof(struct student),1,fp)!=1)
printf("\t error \n");
}
fclose(fp);
system("pause");
}
void read_student() //读取学生信息
{
FILE *fp;
count=0;
printf("\n\t\t\t*******读取学生信息*******\t\t\t\n");
if((fp = fopen("student.txt","rb")) == NULL)
{
printf("Can not open file\n");
if((fp=fopen("student.txt","w"))==NULL)
printf("文件无法打开");
}
fseek(fp,0,SEEK_END);
if(ftell(fp)>0)
{
rewind(fp);
fread(&msg[count],sizeof(struct student),1,fp);
while (!feof(fp))
{
fread(&msg[++count],sizeof(struct student),1,fp);
}
}
if(count)
{
printf("文件导入成功");
}
else
printf("文件导入失败.");
system("pause");
fclose(fp);
}
|
|