| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 447 人关注过本帖
标题:一个程序希望大家指点!~
只看楼主 加入收藏
oyaweio
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-5-10
收藏
 问题点数:0 回复次数:1 
一个程序希望大家指点!~

谁能帮我把这个程序写出插入部分(如新来一个学生的信息把它插入进去)~谢谢各位高手了哇!!!

#include "Stdio.h"
#include "Conio.h"
void menu(void);
void input(void);
void display(void);
void modify(void);
void _delete(void);
void find(void);


typedef struct student
{
int no;
char name[20];
char sex[6];
int age;
char major[20];
int flag;
/*flag用来表示状态,用0表示正常,1表示处于删除状态*/
}STUDENT;


int main()
{
menu();
}


void menu()
/*以下是菜单程序*/
{ char x;
while(1)
{
clrscr();
printf("\n\n\t\tThere is some information about students");
printf("\n\n\n\t\t1.input data");
printf("\n\n\n\t\t2.display data");
printf("\n\n\n\t\t3.modify a student");
printf("\n\n\n\t\t4.delete a student");
printf("\n\n\n\t\t5.find the information of a student");
printf("\n\n\n\t\t0.exit");
x=getch();
if(x=='0') break;
if(x=='1') input();
if(x=='2') display();
if(x=='3') modify();
if(x=='4') _delete();
if(x=='5') find();
}
return 0;
}


void input() /*输入函数*/
{
FILE *fp;
STUDENT stu1;
fp=fopen("file","ab");
if(fp==NULL)
{
fp=fopen("file","wb");
if(fp==NULL)
{
printf("can't open a file");
exit(0);
}
}
while(1) /*输入学生信息*/
{
printf("\nno?");
scanf("%d",&stu1.no);
if(stu1.no==0) break; /*当学号为0时结束输入*/
printf("\nname?");
scanf("%s",stu1.name);
printf("\nsex?");
scanf("%s",stu1.sex);
printf("\nage?") ;
scanf("%d",&stu1.age);
printf("\nmajor?");
scanf("%s",stu1.major);
stu1.flag=0; /*令此时的学生信息为正常状态*/
fwrite(&stu1,sizeof(stu1),1,fp); /*把输入的信息写入文件*/
}
fclose(fp);
return;
}

void display() /*输出函数*/
{
FILE *fp;
STUDENT stu1;
fp=fopen("file","rb");
while(!feof(fp))
{
stu1.no=0; /*使学生学号初始状态为0,当入读成功时不等于0*/
fread(&stu1,sizeof(stu1),1,fp);
if(stu1.no==0) break;
printf("\n\n%d %s %s %d %s",stu1.no,stu1.name,stu1.sex,stu1.age,stu1.major);
}
fclose(fp);
getch();
}


/*以下程序将要修改学生的信息先存放在结构体中,然后对修改的学号进行比较,将要
修改学号位置以前的学生信息写到临时文件中,然后将修改学生的信息写入,最后写入剩
下的学生信息,然后再覆盖原文件,当要修改的学号不存在时即实现在文件最后的插入功能*/
void modify() /*修改函数*/
{
FILE *fp1,*fp2; /*用fp2指向要修改的学生信息*/
int num,offset;
STUDENT stu1,stu2;
fp1=fopen("file","rb");
if(fp1==NULL)
{
printf("can't open a file");
exit(0);
}
fp2=fopen("temp","wb"); /*建立一个临时文件*/
printf("\nplease input a number that you want to modify \n");
scanf("%d",&num); /*输入要修改的位置*/
printf("\nno?"); /*输入要修改的学生信息*/
scanf("%d",&stu2.no);
printf("\nname?");
scanf("%s",stu2.name);
printf("\nsex?");
scanf("%s",stu2.sex);
printf("\nage?") ;
scanf("%d",&stu2.age);
printf("\nmajor?");
scanf("%s",stu2.major);
stu2.flag=0;

while(fread(&stu1,sizeof(stu1),1,fp1)!=0&&stu1.no!=num)
fwrite(&stu1,sizeof(stu1),1,fp2); /*当读入成功时,先将修改学号位置以前的学生信息写入临时文件*/
fclose(fp2);

fp2=fopen("temp","ab");/*以追加形式重新打开临时文件,以保存已经存放的信息*/
fwrite(&stu2,sizeof(stu1),1,fp2); /*写入要修改的学生信息*/

while(fread(&stu1,sizeof(stu1),1,fp1)!=0)
fwrite(&stu1,sizeof(stu1),1,fp2); /*写入剩下的学生信息*/
fclose(fp1);
fclose(fp2);

fp1=fopen("file","wb"); /*将临时文件的内容放回原文件中*/
if((fp2=fopen("temp","rb"))==NULL)
{
printf("can't open the file");
exit(0);
}
while(!feof(fp2))
{
stu1.no=0;
fread(&stu1,sizeof(stu1),1,fp2);
if(stu1.no==0) break;
fwrite(&stu1,sizeof(stu1),1,fp1);
}
fclose(fp1);
fclose(fp2);
}

void _delete()
{
FILE *fp1,*fp2;
struct student stu1;
int no;
long offset;
printf("\nPlease input the no of the student that you want to delete");
printf("\nno?");
scanf("%d",&no);
fp1=fopen("file","rb+");
/*以下循环完成将要删除的学生的状态改为1,标志该学生为要删除的学生
此时该学生的信息依然存在于文件中*/
/*注意在这个循环中,要将改过的内容写入文件中,否则就不能完成修改*/
while(!feof(fp1))
{
offset=ftell(fp1);
stu1.no=0;
/*留下循环开始时文件中隐含指针指向的位置*/
fread(&stu1,sizeof(stu1),1,fp1);
if(stu1.no==0) break;
if(stu1.no==no)
{
stu1.flag=1;
fseek(fp1,offset,SEEK_SET); /*文件中隐含有一个指针,每读一次指针后移
一位,因此在写入时应将指针移动至上次读的
位置,用移动指针的函数*/
fwrite(&stu1,sizeof(stu1),1,fp1); /*将修改过的内容存入文件*/
}
}
fclose(fp1);
/*以下段程序功能:将处于正常状态,即flag=0,下的学生信息存入一个临时文件中*/
fp1=fopen("file","rb");
fp2=fopen("tempt","wb");
/*if(fp2==NULL)
{
fp2=fopen("tempt","wb");
if(fp2==NULL) {printf("Can not open the file");exit(0);}
} */
while(!feof(fp1))
{
stu1.no=0;
fread(&stu1,sizeof(stu1),1,fp1);
if(stu1.no==0) break;
if(stu1.flag==0)
fwrite(&stu1,sizeof(stu1),1,fp2);
}
fclose(fp1);
fclose(fp2);
/* 以下段功能:用临时文件中的正常学生信息覆盖原文件中处于删除状态的学生信息*/
fp1=fopen("file","wb");/*用写方式打开文件,便于后面覆盖其内容 */
fp2=fopen("tempt","rb");
while(!feof(fp2))
{
stu1.no=0;
fread(&stu1,sizeof(stu1),1,fp2);
if(stu1.no==0) break;
fwrite(&stu1,sizeof(stu1),1,fp1);
}
fclose(fp1);
fclose(fp2);
remove(fp2);/*临时文件用完以后应该将文件删除,用remove函数实现这一功能*/
}


void find() /*查找函数*/
{
FILE *fp;
struct student stu1;
int num;

fp=fopen("file","rb");
if(fp==NULL) printf("can't open the file");
/*用读的方式打开文件,如果不能打开文件,则返回出错信息*/
printf("\n\n\nPlease input the no of the student you want to find");
printf("\nno=?");
scanf("%d",&num);
/*录入要查找的学生的学号 */
while(!feof(fp))
{

stu1.no=0;
fread(&stu1,sizeof(stu1),1,fp);
if(stu1.no==0) break;
if(stu1.no==num)
printf("\n\n%d %s %s %d %s",stu1.no,stu1.name,stu1.sex,stu1.age,stu1.major);
}
/*当读文件中的学生信息是找到了与输入的学号相同的学生,则打印出这个学生的信息*/
fclose(fp);
getch();
}

2006-05-10 21:53
论坛
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1372
专家分:0
注 册:2006-3-27
收藏
得分:0 
#include "Stdio.h"
#include "Conio.h"

日出东方,唯我不败! 做任何东西都是耐得住寂寞,任何一个行业要有十年以上的积累才能成为专家
2006-05-10 22:47
快速回复:一个程序希望大家指点!~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015998 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved