|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
写了个简单的成绩管理系统,非常简单,但是用到了文件操作,下面贴出两个函数,求大神解释一下,问什么在对文件进行写入时会出现问题。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 20
#define MAX 30 //每班最多30个学生
typedef struct gra
{
int C; //C语言与程序设计
int ENG; //英语
int FM; //高数
int UP; //大物
int PAS; //概率论与数理统计
} GRA;
typedef struct student
{
//unsigned int studentClass;
unsigned int studentID;
char studentName[10];
//unsigned int studentTerm;
GRA studentGrade;
} STU;
void Function0()
{
int i, k, classnum, studentnum;
//unsigned int temp, temp1;
char fileCode[N];
FILE *fp;
printf("多少个班级?\n");
scanf("%d", &classnum);
for (i = 0; i < classnum; i++)
{
/*printf("请输入班级编号:\n");
scanf("%u", &temp);
printf("请输入学期编号:\n");
scanf("%u", &temp1);*/
printf("请输入第%d个班级的学生数:\n", i+1);
scanf("%d", &studentnum);
STU *pt = (STU*) calloc(studentnum, sizeof(STU));
STU *ppt = pt; //保存pt的值
printf("请依次输入各个同学的学号、姓名和各科成绩\n");
for (k = 0; k < studentnum; k++, pt++)
{
printf("第%d个学生\n", k+1);
//printf("请依次输入各个同学的学号、姓名和各科成绩\n");
//printf("学号\t姓名\tC语言与程序设计\t大学英语\t高等数学\t大学物理\t概率论与数理统计\n");
//pt->studentClass = temp;
printf("学号:");
scanf("%u", &pt->studentID);
printf("%u\n", pt->studentID);
printf("姓名:");
scanf("%s", pt->studentName);
printf("%s\n", pt->studentName);
//scanf("%u", &(pt->studentTerm));
printf("C语言与程序设计:");
scanf("%d", &pt->studentGrade.C);
printf("%d\n", pt->studentGrade.C);
printf("大学英语:");
scanf("%d", &pt->studentGrade.ENG);
printf("%d\n", pt->studentGrade.ENG);
printf("高等数学:");
scanf("%d", &pt->studentGrade.FM);
printf("%d\n", pt->studentGrade.FM);
printf("大学物理:");
scanf("%d", &pt->studentGrade.UP);
printf("%d\n", pt->studentGrade.UP);
printf("概率论与数理统计:");
scanf("%d", &pt->studentGrade.PAS);
printf("%d\n", pt->studentGrade.PAS);
}
printf("请按格式输入文件名,文件名格式为:班级编号_学期编号,例如:04121517_201601\n");
getchar();
gets(fileCode);
//将.bin连接在fileCode后面,组成文件名
strcat(fileCode, ".bin");
fp = NULL;
if ((fp = fopen(fileCode, "wb")) == NULL)
{
printf("文件初始化错误!\n");
exit(1);
}
fwrite(pt, sizeof(STU), k, fp);
fclose(fp);
pt = ppt;
free(pt);
}
}
void Function1()
{
//char fileCode[N];
int flag = 0;
int count = 0, i;
unsigned int temp = 0;
FILE *fp = NULL;
printf("请输入希望修改信息的学生的班级编号_学期编号:\n");
fp = OpenProject();
//将文件内所有数据读入新开的结构体中进行数据修改,再放回原文件中
STU *pt = (STU *)calloc(MAX, sizeof(STU));
STU *ppt = pt;
fread(pt, sizeof(STU), MAX, fp);
//printf("%u\n", pt->studentID);
//printf("%s\n", pt->studentName);
printf("请输入希望修改信息的学生的学号:\n");
in3: scanf("%u", &temp);
for (i = 0; i <= count-1; i++, pt++)
{
if (temp == pt->studentID)
{
flag = 1;
break;
}
fseek(fp, sizeof(STU), SEEK_SET);
}
if (flag == 1)
{
printf("请重新输入该学生的各科成绩:\n");
scanf("%d", &pt->studentGrade.C);
scanf("%d", &pt->studentGrade.ENG);
scanf("%d", &pt->studentGrade.FM);
scanf("%d", &pt->studentGrade.UP);
scanf("%d", &pt->studentGrade.PAS);
}
else
{
printf("不存在此学号,请重新输入!");
goto in3;
}
fwrite(pt, sizeof(STU), count, fp);
rewind(fp);
pt = ppt;
free(pt);
fclose(fp);
}
在Function0中,我在把pt内的数据写到文件中前,特意输出来看数据是不是正确的,然而数据写到文件中去就出问题了。
Funtion1()中读取的文件数据也就错了,求大神解答。 |
|