#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
typedef struct book{
char name[16];
char id[8]; //存八位编号
int loaned; //借出
int num; //总数
double price; //书价格
char publisher[32]; //出版社
char author[32]; //作者
struct book*next;
}book;
typedef struct student{
char id[8]; //学号
char name[16]; //姓名
int overdue; //逾期数
char borrow_book_name[5][8]; //借的书的名字,最大为5,用编号存
int borrow_book_num=0; //借书数
struct student*next;
}student;
book* Initialisation_book();
student*Initialisation_student();
student* Is_student_id(char *id);
int main(){
book *Head=Initialisation_book();
student*Student=Initialisation_student();
char student_id[16]={0};
printf("请输入你的学号:");
scanf("%s",student_id);
if(Is_student_id(student_id)!=NULL){
student*p=Is_student_id(student_id);
printf("%s\n",p->borrow_book_name[0]);
}
return 0;
}
student* Is_student_id(char *id){
student sd,*p=NULL;
FILE*fp=NULL;
fp=fopen("student.dat","r");
if(fp==NULL){
printf("打开文件失败!");
exit(1);
}
int flag=0;
fseek(fp,0,SEEK_SET);
while(!feof(fp)){
fread(&sd,sizeof(student),1,fp);
if(strcmp(sd.id,id)==0){
p=&sd;
flag=1;
break;
}
}
//printf("%s\n",p->borrow_book_name[0]);
return p;
}
book* Initialisation_book(){
book *tail=NULL,*Head=NULL,*sd;
FILE*fp=NULL;
long size;
fp=fopen("library.dat","r");
if(fp==NULL){
printf("打开文件失败!");
exit(1);
}
fseek(fp, 0, SEEK_END); // 将文件指针移动到文件末尾
size = ftell(fp); //获取文件大小
fseek(fp, 0, SEEK_SET); // 将文件指针移回文件开头
while(ftell(fp) != size){
sd=(book*)malloc(sizeof(book));
sd->next=NULL;
if(fread(sd,sizeof(book),1,fp) != 1){
free(sd);
break;
}
if(Head){
tail->next=sd;
tail=tail->next;
}else{
Head=sd;
tail=sd;
}
}
fclose(fp);
tail=NULL;
return Head;
}
student*Initialisation_student(){
student *tail=NULL,*Head=NULL,*sd;
FILE*fp=NULL;
long size;
fp=fopen("student.dat","r");
if(fp==NULL){
printf("文件打开失败!");
return NULL;
}
fseek(fp,0,SEEK_END);
size = ftell(fp);
fseek(fp,0,SEEK_SET);
while(ftell(fp)!=size){
sd=(student*)malloc(sizeof(student));
sd->next=NULL;
if(fread(sd,sizeof(student),1,fp)!=1){
free(sd);
break;
}
if(Head){
tail->next=sd;
tail=tail->next;
}else{
Head=sd;
tail=sd;
}
}
fclose(fp);
tail=NULL;
return Head;
}
为什么在isstudent里面我已经指到东西了,在函数里面输出是正常的但是在返回在外面就不行了
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- qzjhjxj 2023-05-04 15:13关注
这么改,改动处见注释,供参考:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<windows.h> typedef struct book { char name[16]; char id[8]; //存八位编号 int loaned; //借出 int num; //总数 double price; //书价格 char publisher[32]; //出版社 char author[32]; //作者 struct book* next; }book; typedef struct student { char id[8]; //学号 char name[16]; //姓名 int overdue; //逾期数 char borrow_book_name[5][8]; //借的书的名字,最大为5,用编号存 int borrow_book_num = 0; //借书数 struct student* next; }student; book* Initialisation_book(); student* Initialisation_student(); student* Is_student_id(char* id); int main() { book* Head = Initialisation_book(); student* Student = Initialisation_student(); char student_id[16] = { 0 }; printf("请输入你的学号:"); scanf("%s", student_id); student* p = Is_student_id(student_id); // 修改 if (p) { //if (Is_student_id(student_id) != NULL) 修改 //student* p = Is_student_id(student_id); 修改 printf("%s\n", p->borrow_book_name[0]); } return 0; } student* Is_student_id(char* id) { student *sd = (student*)malloc(sizeof(student)), * p = NULL; //修改 FILE* fp = NULL; fp = fopen("student.dat", "r"); if (fp == NULL) { printf("打开文件失败!"); return p; //exit(1); 修改 } int flag = 0; fseek(fp, 0, SEEK_SET); while (1) { //while (!feof(fp)) 修改 if (fread(sd, sizeof(student), 1, fp) == 1) { //修改 if (strcmp(sd->id, id) == 0) { p = sd; flag = 1; break; } } else //修改 break; //修改 } fclose(fp); // 修改 //printf("%s\n",p->borrow_book_name[0]); return p; } book* Initialisation_book() { book* tail = NULL, * Head = NULL, * sd; FILE* fp = NULL; long size; fp = fopen("library.dat", "r"); if (fp == NULL) { printf("打开文件失败!"); exit(1); } fseek(fp, 0, SEEK_END); // 将文件指针移动到文件末尾 size = ftell(fp); //获取文件大小 fseek(fp, 0, SEEK_SET); // 将文件指针移回文件开头 while (ftell(fp) != size) { sd = (book*)malloc(sizeof(book)); sd->next = NULL; if (fread(sd, sizeof(book), 1, fp) != 1) { free(sd); break; } if (Head) { tail->next = sd; tail = tail->next; } else { Head = sd; tail = sd; } } fclose(fp); tail = NULL; return Head; } student* Initialisation_student() { student* tail = NULL, * Head = NULL, * sd; FILE* fp = NULL; long size; fp = fopen("student.dat", "r"); if (fp == NULL) { printf("文件打开失败!"); return NULL; } fseek(fp, 0, SEEK_END); size = ftell(fp); fseek(fp, 0, SEEK_SET); while (ftell(fp) != size) { sd = (student*)malloc(sizeof(student)); sd->next = NULL; if (fread(sd, sizeof(student), 1, fp) != 1) { free(sd); break; } if (Head) { tail->next = sd; tail = tail->next; } else { Head = sd; tail = sd; } } fclose(fp); tail = NULL; return Head; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 有关wireshark抓包的问题
- ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
- ¥15 向数据表用newid方式插入GUID问题
- ¥15 multisim电路设计
- ¥20 用keil,写代码解决两个问题,用库函数
- ¥50 ID中开关量采样信号通道、以及程序流程的设计
- ¥15 U-Mamba/nnunetv2固定随机数种子
- ¥15 vba使用jmail发送邮件正文里面怎么加图片
- ¥15 vb6.0如何向数据库中添加自动生成的字段数据。
- ¥20 在easyX库下编写C语言扑克游戏跑的快,能实现简单的人机对战