代码实例(学生证管理程序)

上传人:hs****ma 文档编号:561988463 上传时间:2023-10-31 格式:DOC 页数:13 大小:27KB
返回 下载 相关 举报
代码实例(学生证管理程序)_第1页
第1页 / 共13页
代码实例(学生证管理程序)_第2页
第2页 / 共13页
代码实例(学生证管理程序)_第3页
第3页 / 共13页
代码实例(学生证管理程序)_第4页
第4页 / 共13页
代码实例(学生证管理程序)_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《代码实例(学生证管理程序)》由会员分享,可在线阅读,更多相关《代码实例(学生证管理程序)(13页珍藏版)》请在金锄头文库上搜索。

1、*/#include #include #include #include /* 字定义函数声明 */void Add();/增加记录void Delete();/删除记录void Search();/查找记录void Alter();/修改记录void Views();/查看所有记录void delete_all(); /清除所有记录int check(char strID); /查找文件记录中已经存在的学生ID/* 定义学生信息结构体 */struct student /学生信息(学号,姓名,性别,籍贯,电话,住址,民族); char ID10; char name10; char sex

2、4; char nativePlace10; char phone13; char nation6;struct student stu;FILE *fp;/定义文件指针/* main() 主函数 */void main()int flag;while(flag!=7)printf(/n/t/t 【 请选择您所要的操作 】 /n);printf(/t/t*/n);printf(/t/t 1、增加记录 4、修改记录 /n);printf(/t/t 2、删除记录 5、清除所有记录 /n);printf(/t/t 3、查询记录 6、所有记录列表 /n);printf(/t/t 按任意键退出 /n);

3、printf(/t/t*/n);scanf(%d,&flag);switch(flag)case 1:Add();/增加记录getch();system(cls);break;case 2:Delete();/删除记录getch();system(cls);break;case 3:Search();/查找记录getch();system(cls);break;case 4:Alter();/修改记录getch();system(cls);break;case 5:delete_all();/清空所有记录getch();system(cls);break;case 6:Views();/查看所

4、有记录getch();system(cls);break;default:exit(0);/* 插入新记录 */void Add() if(fp=fopen(StudentMessage,ab+)=NULL)printf(学生信息文件打开失败!);exit(1);elseint check_add(char stuID);long flength,offset;int k=0,t;/t用来接收check()函数的传值char str53;fseek(fp,0,SEEK_END);flength=ftell(fp)/sizeof(stu); /统计文件中有多少条记录printf(/t请输入要插入

5、的学生的信息:/n);input_message:printf(/t学号:);scanf(%s,&stu.ID);t=check_add(stu.ID); if(t=1)printf(/t 该学号已经存在,不允许重复插入!请重新输入一个学号。/n);goto input_message; /跳转到“input_message”标记处继续执行printf(/t姓名:);scanf(%s,&stu.name);printf(/t性别:);scanf(%s,&stu.sex);printf(/t籍贯:);scanf(%s,&stu.nation);printf(/t电话:);scanf(%s,&st

6、u.phone);printf(/t名族:);scanf(%s,&stu.nativePlace);rewind(fp);/检测文件中是否有空字符串,如果有则在此处插入新记录,如果没有则插入到文件尾while(!feof(fp)fread(&str,sizeof(stu),1,fp);/检测到空字符串,则将fp所指向的文件关闭,以读写方式重新打开if(strcmp(str,)=0)fclose(fp);fp=fopen(StudentMessage,rb+);break;k+;offset=sizeof(stu)*k;fseek(fp,offset,SEEK_SET);fwrite(&stu,

7、sizeof(stu),1,fp);fclose(fp);printf(插入数据成功!/n);/* 检测将要插入的学号是否已存在文件记录中 */int check(char stuID10)int id=0,k=1;FILE *fpp;struct student checkID;if(fpp=fopen(StudentMessage,rb)=NULL) /以只读方式打开一个二进制文件StudentMessageprintf(文件打开失败!/n);exit(1);/打开失败,返回系统while(!feof(fpp)fread(&checkID,sizeof(stu),1,fpp);/检测用户输

8、入的学号是否已经存在,若存在则终止程序的执行if(strcmp(checkID.ID,stuID)=0) break;id+;fclose(fpp);return id*k;/* 检测输入的学号是否能插入 */int check_add(char stuID10)int id;FILE *fpp;struct student checkID;if(fpp=fopen(StudentMessage,rb)=NULL) /以只读方式打开一个二进制文件StudentMessageprintf(文件打开失败!/n);exit(1);/打开失败,返回系统while(!feof(fpp)fread(&ch

9、eckID,sizeof(stu),1,fpp);/检测用户输入的学号是否已经存在,若存在则终止程序的执行if(strcmp(checkID.ID,stuID)=0) id=1; break;elseid=0;fclose(fpp);return id;/* 根据用户输入的学号删除记录 */void Delete() printf(删除相应记录!/n);if(fp=fopen(StudentMessage,rb+)=NULL)/以可读可写方式打开一个二进制文件printf(文件打开失败!/n);return;elsechar message10; /接收用户需要删除的学号int i;long

10、flength;fseek(fp,0,SEEK_END); /把文件指针移到文件尾flength=ftell(fp)/sizeof(struct student); /计算文件中有多少条记录printf(/t请输入您要删除的学号:);scanf(%s,message);i=check(message);long offset=sizeof(stu)*i;/记录要删除的学号的位置if(flength=0)printf(文件记录为空!/n);return; else if(iflength)printf(没有该学生信息!/n);return;else/将要删除的字符串全部置为空strcpy(stu.ID,);strcpy(stu.name,);strcpy(stu.sex,);strcpy(stu.nation,);strcpy(stu.phone,);strcpy(stu.nativePlace,);fseek(fp,offset,SEEK_SET); /移动指针到要删除的学号的位置fwrite(&stu,sizeof(st

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业/管理/HR > 销售管理

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号