26行fclose(fp)显示错误expected constructor destructor or type

#include
#include
#include
#include
typedef struct pnode //结构体定义 用于存放学生信息的节点
{
char name [10]; //姓名
char num[16]; //学号
char dor[30]; //房号
char group[20]; //班级
}student;
char filename[20]; //文件名
FILE fp; //指向文件的指针
void creat() // 创建一个二进制文件 用于存放学生数据
{ student *person;
person=(student *)malloc(sizeof(student)); //为节点分配内存
printf("\n 请您输入文件名:\n");
scanf("%s",filename);
if((fp=fopen(filename,"w+"))==NULL)

{printf("\n 您没有输入文件名 不能找到文件");exit(0);}
printf("\n请输入学生的姓名 学号 房号 班级 用空格隔开 以#结束\n");
scanf("%s",person->name);
while(strcmp(person->name,"#")) //该循环用于控制学生信息的录入 遇#结束 { scanf("%s %s %s",person->num,person->dor,person->group);
fprintf(fp,"%-10s%-10s%-10s%-10s\n",person->name,person->num,person->dor,person->group);scanf("%s",person->name);
}
fclose(fp);
}
void readfile() //文件读取函数

{ printf("\n请输入文件名:\n");
scanf("%s",filename); //此处输入为文件的路径

if((fp=fopen(filename,"r+"))==NULL)
{printf("\n无法打开该文件:\n");exit(0);}
fclose(fp);
}
void output() //输出函数 用于输出文件的全部信息
{ student *person;
long offset1,offset2;
char name1[10],name2[10],name3[10];
char num1[16],num2[16],num3[16];
char dor1[30],dor2[30],dor3[30];
char group1[20],group2[20],group3[20];
person=(student *)malloc(sizeof(student));
if((fp=fopen(filename,"r"))==NULL)
{printf("\n 无法打开该文件");exit(0);}
while(!feof(fp)) //此循环用于对文件数据中关键字学号进行从小到大冒泡排序
{ int a,b,c,d; //a为循环结束判定变量 b,c,d用于起泡排序学号交换的替换
if((fp=fopen(filename,"r+"))==NULL) //冒泡排序
{printf("\n 无法打开该文件");exit(0);}
while(!feof(fp)) // 外部循环
{
while(!feof(fp)) //内部循环
{
offset1=ftell(fp); //获取文件内部当前指针位置fscanf(fp,"%s%s%s%s\n",person->name,person->num,person->dor,person->group);
strcpy(name1,person->name);
strcpy (num1,person->num);
strcpy(dor1,person->dor);
strcpy(group1,person->group);
if(feof(fp))

break; //文件结束 跳出循环
offset2=ftell(fp); //获取文件内部下一指针位置fscanf(fp,"%s%s%s%s\n",person->name,person->num,person->dor,person->group);
strcpy (name2,person->name);
strcpy(num2,person->num);
strcpy(dor2,person->dor);
strcpy(group2,person->group);
b=strlen(num1);
c=strlen(num2);
d=strcmp(num1,num2);
if(b==c&&d>0) //如果 学号1大于等于学号2 则交换全部数据
{ strcpy(name3,name1);
strcpy(name1,name2);
strcpy(name2,name3);
strcpy(num3,num1);
strcpy(num1,num2);
strcpy(num2,num3);
strcpy(dor3,dor1);
strcpy(dor1,dor2);
strcpy(dor2,dor3);
strcpy(group3,group1);
strcpy(group1,group2);
strcpy(group2,group3);
fseek(fp,offset1,SEEK_SET); //将指针移动offset1个字节
strcpy(person->name,name1);
strcpy(person->num,num1);
strcpy(person->dor,dor1);
strcpy(person->group,group1);
fprintf(fp,"%-10s%-10s%-10s%-10s\n",
person->name,person->num,person->dor,person->group); //排序后写入文件
strcpy(person->name,name2);
strcpy(person->num,num2);
strcpy( person->dor,dor2);
strcpy(person->group,group2);
fprintf(fp,"%-10s%-10s%-10s%-10s\n",person->name,person->num,person->dor,person->group);//排序后写入文件
}
fseek(fp,offset2,SEEK_SET);

//将文件位置指针从文件头向前移动offset2个字节
}
rewind(fp); //指向头文件
while(!feof(fp)) //此循环用于判断学号是否有序
{ offset1=ftell(fp);
fscanf(fp,"%s%s%s%s\n",person->name,person->num,person->dor,person->group);
strcpy (num1,person->num);
if(feof(fp))
{a=1;break;}
offset2=ftell(fp);
fscanf(fp,"%s%s%s%s\n",person->name,person->num,person->dor,person->group);
strcpy(num2,person->num);
b=strlen(num1);
c=strlen(num2);
d=strcmp(num1,num2);
if(b==c&&d<=0)
a=1; //若a为1 则有序
else
{ a=0; //a为0则无序
break;}
fseek(fp,offset2,SEEK_SET);
}
if(a==1)
break; //a为1 说明学号已经有序 跳出循环
rewind(fp);
}
if(a) break; //若a为1,则学号已有序排列,跳出总循环,输出文件
}
rewind(fp);
printf("\n
***************************************************************\n");
printf("%40s\n","输出的记录如下(已按学号排序)\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","学号","房号","班级");
while(!feof(fp)) //次循环用于输出文件
{
fscanf(fp,"%s%s%s%s\n",person->name,person->num,person->dor,person->group);
printf("%-20s%-20s%-20s%-20s\n",
person->name,person->num,person->dor,person->group);
}
fclose(fp);
printf("************************************************************\n\n");
}
void namesearch() // 按姓名搜索函数
{ int k=0;
char namekey[10];
student *person;
person=(student *)malloc(sizeof(student));
printf("\n 请输入您要查找的姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"rb"))==NULL)
{printf("\n 无法打开文件");exit(0);}
while(!feof(fp))
{
fscanf(fp,"%s %s %s %s\n",person->name,person->num,person->dor,person->group);
if(!strcmp(namekey,person->name))

{ printf("\n\n 已经为您找到 以下是记录:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","学号","房号","班级");
printf("%-20s%-20s%-20s%-20s\n",person->name,person->num,person->dor,person->group);
k=1;
}
}
if(!k)
printf("\n\n 没有关于此姓名的任何信息! \n");
fclose(fp);
}
void numsearch() //按学号搜索函数
{ int k=0;
char xhkey[16];
student *person;
person=(student *)malloc(sizeof(student));
printf("\n 请您输入需要查找的学号:");
scanf("%s",xhkey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\n 无法打开文件 ");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%s %s %s %s\n",person->name,person->num,person->dor,person->group);

if(!strcmp(xhkey,person->num))

{ printf("\n\n 已经为您找到 以下是记录:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","学号","房号","班级");
printf("%-20s%-20s%-20s%-20s\n",person->name,person->num,person->dor,person->group);
k=1;
}
}
if(!k)
printf("\n\n 没有关于该学号的任何信息\n");
fclose(fp);
}
void dorsearch () //按房号搜索函数
{ int k=0;
char fhkey[30];
student *person;
person=(student *)malloc(sizeof(student));
printf("\n 请您输入想要查找的房号");
scanf("%s",fhkey);
if((fp=fopen(filename,"rb"))==NULL)
{ printf("\n 无法打开文件");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%s %s %s %s\n",person->name,person->num,person->dor,person->group);
if(!strcmp(fhkey,person->dor))

{ printf("\n\n 已经为您找到 以下是记录:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","学号","房号","班级");
printf("%-20s%-20s%-20s%-20s\n",person->name,person->num,person->dor,person->group);
k=1;
}
}
if(!k)
printf("\n\n 没有关于此房号的任何信息\n");
fclose(fp);
}
void add() //插入函数 用于像已有文件插入一条新的学生信息记录
{ student *person;
person=(student *)malloc(sizeof(student));
if((fp=fopen(filename,"a"))==NULL)
{ printf("\n 无法打开文件");exit(0);}
printf("\n 请您输入学生姓名 学号 房号 班级 \n");
scanf("%s %s %s %s",person->name,person->num,person->dor,person->group);
fprintf(fp,"%-10s%-10s%-10s%-10s\n",person->name,person->num,person->dor,person->group);
fclose(fp);
}
void modify() //更新函数 用于修改指定学生姓名的记录
{ int k=0;
long offset;
char namekey[10];
student *person;
person=(student *)malloc(sizeof(student));
printf("\n 请您输入想要更改的学生的姓名 :");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{ printf("\n 无法打开文件"); exit(0);}
while(!feof(fp))
{ offset=ftell(fp);
fscanf(fp,"%s %s %s %s\n",person->name,person->num,person->dor,person->group);
if(!strcmp(namekey,person->name)) //比较是否相同 如
{k=1; break;}
}
if(k) //相同 输出记录并进行修改
{ printf("\n 记录输出如下:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","学号","房号","班级");
printf("%-20s%-20s%-20s%-20s\n",person->name,person->num,person->dor,person->group);
printf("\n 请您输入新的学生姓名 学号 房号 班级:\n");
scanf("%s %s %s %s",person->name,person->num,person->dor,person->group);
fseek(fp,offset,SEEK_SET);
fprintf(fp,"%-10s%-10s%-10s%-10s\n",person->name,person->num,person->dor,person->group);
}
else
printf("\n 没有关于该姓名的任何记录\n");
fclose(fp);
}
void deleted() //删除函数 用于删除指定学生姓名的记录
{
int k=0;
char m;
long offset;
char namekey[10];
student *person;
person=(student *)malloc(sizeof(student));
printf("\n请您输入想要删除的姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n 无法打开文件 ");
exit(0);
}
while(!feof(fp)) //此循环遍历整个文件 查找需要删除的记录
{
offset=ftell(fp);
fscanf(fp,"%s %s %s %s\n",person->name,person->num,person->dor,person->group);
if(!strcmp(namekey,person->name))
{ k=1; break; }
}
if(k)
{ printf("\n 记录输出如下:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","学号","房号","班级");
printf("%-20s%-20s%-20s%-20s\n",person->name,person->num,person->dor,person->group); printf("\n 您真的确定要删除?y/n?");
scanf("%s",&m);
if(m=='y') //删除确认按钮
{ fseek(fp,offset,SEEK_SET); //删除记录
fprintf(fp,"%-10s%-10s%-10s%-10s\n","","","","");
}
else rewind(fp);
}
else printf("\n 没有关于该姓名的任何记录\n");
fclose(fp);
}
void main() //主函数
{ int m,flag=1; // m用于控制菜单的选择项 flag用于控制菜单弹出
while(flag)
{
printf("%40s\n","海南大学宿舍管理系统");
printf("--------------------------------------------------------------------------------\n")
printf("\t\t0 ------------ 创建文件\n");
printf("\t\t1 ------------ 打开文件\n");
printf("\t\t2 ------------ 姓名查询\n");
printf("\t\t3 ------------ 学号查询\n");
printf("\t\t4 ------------ 房号查询\n");
printf("\t\t5 ------------ 修改记录\n");
printf("\t\t6 ------------ 删除记录\n");
printf("\t\t7 ------------ 增加纪录\n");
printf("\t\t8 ------------ 输出记录\n");
printf("\t\t9 ------------ 退出\n");
printf("--------------------------------------------------------------------------------\n");
printf("\t 请输入0-9选择您的操作:\n");
scanf("%d",&m);
switch(m)
{
case 0:creat();
break;
case 1:readfile();
break;
case 2:namesearch ();
break;
case 3:numsearch();
break;
case 4:dorsearch();

break;
case 5:modify();
break;
case 6: deleted();
break;
case 7:add();
break;
case 8:output();

break;
case 9:exit(0);
default:break;
}
}
}

2个回答

求各位大佬帮忙检查一下

有一个比较严重的错误:

 FILE fp; //指向文件的指针

应该是:

 FILE *fp; //指向文件的指针

粘贴上来的代码,格式容易混乱,您可以再自行调试一下。

用心回答每个问题,如果对您有帮助,请采纳答案好吗,谢谢!

qq_37408011
hht238000 改成这个之后还是会显示错误expected constructor,destructor,or type.conversion before'('token
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问