#include<stdlib.h>
#include<string.h>
typedef struct St1 //定义结构体
{ char IDNumber[16]; //学号
char FamilyName[16]; //姓
char GivenName[16]; //名
float waiyu; //外语成绩
float gaoshu; //高数成绩
float cyuyan; //C语言成绩
float mazhe; //马哲成绩
int sex; //性别(0代表“女”,1代表“男”)
int BirthYear; //出生年
int BirthMonth; //出生月
int BirthDay; //出生日
float score[4]; //成绩
float sum; //总分
float average; //平均分
struct St1 *next;
}StudentNode;
StudentNode St1[500]; //定义500
char FamilyName[16]; //姓
char GivenName[16]; //名
char IDNumber[16];
void logon() //登陆函数
{
printf("\n\n\n\t\t\t 学生信息管理系统\n\n");
system("pause");
}
void filename()
{
system("cls");
printf("请输入你要操作的文件名:");
scanf("\t%s",filename);
}
//*********************************************************************//
int menu_select()
{
char c;
do{
system("cls");
printf("\t\t 学生信息管理系统 \n");
printf("\t\t 1. Enter list \n");
printf("\t\t 2. Print list(有过1才能2) \n");
printf("\t\t 3. Search record \n");
printf("\t\t 4. Compute the record \n");
printf("\t\t 5. Change a record from list \n");
printf("\t\t 6. Delete a record from list \n");
printf("\t\t 0. Quit \n");
printf("\t\tPlease choose number(0-6):");
c=getchar();
}while(c<'0'||c>'6');
return (c-'0');
}
//*********************************************************************//
void add() //成绩的录入函数
{
FILE *fp;
int i=0,n=0;
char sign='y';
system("cls"); //清屏
StudentNode St1[500];
while(sign!='n'&&sign!='N')
{
printf("----学生基本情况录入----\n");
printf("姓:");
scanf("\t%s",St1[i].FamilyName);
printf("名:");
scanf("\t%s",St1[i].GivenName);
printf("性别(0代表女,1代表男):");
scanf("\t%d",St1[i].sex);
printf("学号:");
scanf("\t%s",St1[i].IDNumber);
printf("出生年:");
scanf("\t%d",St1[i].BirthYear);
printf("出生月:");
scanf("\t%d",St1[i].BirthMonth);
printf("出生日:");
scanf("\t%d",St1[i].BirthDay);
printf("外语:");
scanf("\t%f",&St1[i].waiyu);
printf("高数:");
scanf("\t%f",&St1[i].gaoshu);
printf("c语言:");
scanf("\t%f",&St1[i].cyuyan);
printf("马哲:");
scanf("\t%f",&St1[i].mazhe);
printf("是否继续添加?(y/n)");
scanf("\t%c",&sign);
i++;
n=n+1;
system( "cls");
}system("pause");
if((fp=fopen("studata.txt","a"))==NULL)
{
printf("cannot open the files\n");
system("pause");
return; //如果错误就返回
}
for(i=0;i<n;i++)
{
fprintf(fp,"%s %s %d %s %d %d %d %f %f %f %f\n",St1[i].FamilyName,St1[i].GivenName,St1[i].sex,St1[i].IDNumber,St1[i].BirthYear,St1[i].BirthMonth,St1[i].BirthDay,St1[i].waiyu,St1[i].gaoshu,St1[i].cyuyan,St1[i].mazhe);
}
printf("file write error\n");
fclose(fp); //关闭文件
system("pause");
}
//*********************************************************************//
void scan() //显示记录
{
StudentNode St1[500];
void Change(); //声明修改函数//
char sign='y';
FILE *fp;
system("cls");
if((fp=fopen("studata.txt","r"))==NULL) //以2进制方式读取文件信息//
{
printf("cannot open the files\n");
system("pause");
return;
}
int i=0;
while(fscanf(fp,"%s %s %d %s %d %d %d %f %f %f %f",St1[i].FamilyName,St1[i].GivenName,St1[i].sex,St1[i].IDNumber,St1[i].BirthYear,St1[i].BirthMonth,St1[i].BirthDay,St1[i].waiyu,St1[i].gaoshu,St1[i].cyuyan,St1[i].mazhe)!=EOF)
{
printf("姓 名 性别 学号 年 月 日 外语 高数 C语言 马哲\n");
printf("%s %s %d %s %d %d %d %f %f %f %f\n",St1[i].FamilyName,St1[i].GivenName,St1[i].sex,St1[i].IDNumber,St1[i].BirthYear,St1[i].BirthMonth,St1[i].BirthDay,St1[i].waiyu,St1[i].gaoshu,St1[i].cyuyan,St1[i].mazhe);
printf("\n");
i++;
}
system("pause");
system("cls");
printf("是否需要修改?(y/n)");
scanf("\t%c",&sign);
if(sign!='n'&&sign!='N') //选择是否在浏览后进行修改//
Change();
system("pause");
}
以上是代码的一部分,在运行输入性别的时候会出现标题提示,如果将性别删去可以运行。
是野指针还是越界之类的情况吗?
求指导。
3 个解决方案
#1
单步调试
#2
scanf("\t%s", filename); 这里的filename并不是个字符数组或缓冲区,而是一个函数指针,实际指向filename函数,运行时将会崩溃
#3
scanf("\t%d", St1[i].sex); 要改成: scanf("\t%d", &St1[i].sex);
同理其他%d格式的地方
同理其他%d格式的地方
#1
单步调试
#2
scanf("\t%s", filename); 这里的filename并不是个字符数组或缓冲区,而是一个函数指针,实际指向filename函数,运行时将会崩溃
#3
scanf("\t%d", St1[i].sex); 要改成: scanf("\t%d", &St1[i].sex);
同理其他%d格式的地方
同理其他%d格式的地方