我的管理系统怎们创建不起文件呢? 高手帮忙看哈
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define N 100
struct student
{
char num[5];
char name[10];
char sex[5];
char age[5];
char xueli[10];
char gongzi[5];
char address[10];
char tel[11];
};
void myprint();
void mycreate();
void mydisplay();
void mysearch();
void sch_num(FILE *fp);
void sch_xueli(FILE *fp);
void myadd();
void mymodify();
void mydelete();
void del_num(FILE *fp);
void del_xueli(FILE *fp);
void main()
{
char choose,yes_no;
do
{
myprint();
printf(" ");
choose=getch();
switch(choose)
{
case '1': mycreate(); break;
case '2': mydisplay(); break;
case '3': mysearch(); break;
case '4': mymodify(); break;
case '5': myadd(); break;
case '6': mydelete(); break;
case '0': break;
default:printf("\n %c error!\n",choose);
}
if(choose=='0')
break;
printf("\n continue(Y/N)?\n");
do
{
yes_no=getch();
}while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
}while(yes_no!='Y'||yes_no!='y');
}
void myprint()
{
printf("\n\n\n\n\n\n\n\n");
printf(" |----------------------------|\n");
printf(" | shuru(0-5): |\n");
printf(" |----------------------------|\n");
printf(" | 1--chuanjianxinxi |\n");
printf(" | 2--xianshixinxi |\n");
printf(" | 3--chaxuxinxi |\n");
printf(" | 4--xiugaixinxi |\n");
printf(" | 5--shanchuxinxi |\n");
printf(" | 0--tuichu |\n");
printf(" |----------------------------|\n");
}
void mycreate()
{
int i=1;
struct student temp;
FILE *fp;
fp=fopen("d:\\guanli.txt","w");
if(fp==NULL)
{
printf("\n da kai shi bai!\n");
return;
}
printf("\n shu ru di 1 ge ji lu:\n");
printf(" hao ma(yong #jie su shu ru ):");
do
{
gets(temp.num);
}while(strcmp(temp.num,"")==0);
printf(" xingming(yong # jie su shu ru):");
gets(temp.name);
printf(" xingbie(yong #jie su shu ru):");
gets(temp.sex);
printf(" nian ling(yong # jie su shu ru):");
gets(temp.age);
printf(" xue li(yong # jie su shu ru):");
gets(temp.xueli);
printf(" gongzi(yong # jie su shu ru):");
gets(temp.gongzi);
printf(" zhu zi(yong # jie su shu ru):");
gets(temp.address);
printf(" dianhua(yong # jie su shu ru):");
gets(temp.tel);
while(temp.num[0]!='#'&&temp.name[0]!='#'&&temp.sex[0]!='#'&&temp.age[0]!='#'&&temp.xueli[0]!='#'&&temp.gongzi[0]!='#'&&temp.address[0]!='#'&&temp.tel[0]!='#')
{
fprintf(fp,"%5s%10%5s%5s%10s%5s%5s%11s\n",temp.num,temp.name,temp.sex,temp.age,temp.xueli,temp.address,temp.tel);
i++;
printf("\n shu ru di %d ge ji lu:\n",i);
printf(" hao ma(yong # jie su shu ru):");
do
{
gets(temp.num);
}while(strcmp(temp.num,"")==0);
printf(" xing ming(yong # jie su shu ru):");
gets(temp.name);
printf(" xing bie(yong # jie su shu ru):");
gets(temp.sex);
printf(" nian ling(yong # jie su shu ru):");
gets(temp.age);
printf(" xue li(yong # jie su shu ru):");
gets(temp.xueli);
printf(" gongzi(yong # jie su shu ru):");
gets(temp.gongzi);
printf(" zhu zhi(yong # jie su shu ru):");
gets(temp.address);
printf(" dian hua(yong # jie su shu ru):");
gets(temp.tel);
}
fclose(fp);
}
void mydisplay()
{
int n=0;
struct student temp;
FILE *fp;
fp=fopen("d:\\guanli.txt","r+");
if(fp==NULL)
{
printf("\n da kai wen jian shi bai!\n");
return;
}
clrscr();
printf("haoma xingming xingbie nianling xueli gongzi zhuzi dianhua\n");
while(feof(fp)==0)
{
fscanf(fp,"%5s%10s%5s%5s%10s%5s%10s%11s\n",&temp.num,&temp.name,&temp.sex,&temp.age,&temp.xueli,&temp.gongzi,&temp.address,&temp.tel);
printf("%5s%10s%5s%5s%10s%5s%10s%11s\n",temp.num,temp.name,temp.sex,temp.age,temp.xueli,temp.gongzi,temp.address,temp.tel);
n++;
}
if(n==0)
printf("\n wen jian zhong wu ji lu!\n");
else
printf("\n wen jian zhong gong %d ge ji lu \n",n);
fclose(fp);
}
void mysearch()
{
char c;
FILE *fp;
fp=fopen("d:\\guanli.txt","r+");
if(fp==NULL)
{
printf("\n da kai wen jian shi bai!\n");
return;
}
printf("\n an xue li cha xu(h),an xing ming cha xu(m)?");
c=getch();
if(c=='h'||c=='H')
sch_xueli(fp);
if(c=='m'||c=='M')
sch_num(fp);
else
printf("\n fei fa zi fu!\n");
fclose(fp);
}
void sch_num(FILE *fp)
{
int flag=0,n=0;
char tempnum[10];
struct student temp;
printf("\n shu ru cha xu xue hao:");
gets(tempnum);
while(feof(fp)==0)
{
fscanf(fp,"%5s%10s%5s%5s%10s%5s%10s%11s\n",&temp.num,&temp.name,&temp.sex,&temp.age,&temp.xueli,&temp.gongzi,&temp.address,&temp.tel);
if(strcmp(tempnum,temp.num)==0)
{
if(flag==0)
printf(" haoma xingming xingbie nianling xueli gongzi zhuzi dianhua\n");
printf("%5s%10s%5s%5s%10s%5s%10s%11s\n",temp.num,temp.name,temp.sex,temp.age,temp.xueli,temp.gongzi,temp.address,temp.tel);
flag=1;
}
n++;
}
if(n==0)
printf("\n wen jian wu ji lu!\n");
else
if(flag==0)
printf("\n wen jian zhong wu ci ren!\n");
}
void sch_xueli(FILE *fp)
{
int flag=0,n=0;
char tempxueli[10];
struct student temp;
printf("\n shu ru cha xu xue li:");
gets(tempxueli);
while(feof(fp)==0)
{
fscanf(fp,"%5s%10s%5s%5s%10s%5s%10s%11s\n",&temp.num,&temp.name,&temp.sex,&temp.age,&temp.xueli,&temp.gongzi,&temp.address,&temp.tel);
if(strcmp(tempxueli,temp.xueli)==0)
{
if(flag==0)
printf("haoma xingming xingbie nianling xueli gongzi zhuzi dianhua\n");
printf("%5s%10s%5s%5s%10s%5s%10s%11s\n",temp.num,temp.name,temp.sex,temp.age,temp.xueli,temp.gongzi,temp.address,temp.tel);
flag=1;
}
n++;
}
if(n== 0)
printf("\n wen jian wu ji lu!\n");
else
if(flag==0)
printf("\n wen jian zhong wu ci ren!\n");
}
void mymodify()
{
char c;
int n=0;
struct student *find,temp,record[100],*p;
FILE *fp;
fp=fopen("d:\\guanli.txt","r+");
if(fp==NULL)
{
printf("\n da kai wen jian shi bai!\n");
return;
}
p=record;
while(feof(fp)==0)
{
fscanf(fp,"%5s%10s%5s%5s%10s5s%10s%11s\n",p->num,p->name,p->sex,p->age,p->xueli,p->gongzi,p->address,p->tel);
p++;
n++;
}
fclose(fp);
if(n==0)
{
printf("\n wen jian zhong wu ji lu!\n");
return;
}
printf("\n shu ru xiu gai hao ma:");
gets(temp.num);
for(p=record;p<record+n;p++)
if(strcmp(temp.num,p->num)==0)
{
find=p;
break;
}
if(p==record+n)
{
printf("\n wu ci ren !\n");
return;
}
do
{
printf("\n shu ru zheng que de hao ma:");
do
{
gets(temp.num);
}while(strcmp(temp.num,"")==0);
printf(" shu ru zheng que de xing ming:");
gets(temp.name);
printf(" shu ru zheng que de xing bie:");
gets(temp.sex);
printf(" shu ru zheng que de nian ling:");
gets(temp.age);
printf(" shu ru zheng que de xue li:");
gets(temp.xueli);
printf(" shu ru zheng que de gong zi:");
gets(temp.gongzi);
printf(" shu ru zheng que de zhu zi:");
gets(temp.address);
printf(" shu ru zheng que de dian hua:");
gets(temp.tel);
for(p=record;p<record+n;p++)
if((strcmp(temp.num,p->num)==0)&&(p!=find))
{
printf("\n hao ma chong fu chong xin shu ru(Y/N)?");
do
{
c=getch();
}while(c!='Y'&&c!='y'&&c!='N'&&c!='n');
putchar('\n');
break;
}
if(p==record+n)
{
*find=temp;
break;
}
}while(c=='Y'||c=='y');
fp=fopen("d:\\guanli.txt","r+");
if(fp==NULL)
{
printf("\n da kai wen jian zhi bai!\n");
return;
}
for(p=record;p<record+n;p++)
fprintf(fp,"%5s%10s%5s%5s%10s%5s%10s%11s%\n",p->num,p->name,p->sex,p->age,p->xueli,p->gongzi,p->address,p->tel);
fclose(fp);
}
void myadd()
{
char c;
int n=0;
struct student temp,record[N],*p;
FILE *fp;
fp=fopen("d:\\guanli.txt","r+");
if(fp==NULL)
{
printf("\n da kai wen jian shi bai!\n");
return;
}
p=record;
while(feof(fp)==0)
{
fscanf(fp,"%5s%10s%5s%5s%10s%5s%10s%11s\n",p->num,p->name,p->sex,p->age,p->xueli,p->gongzi,p->address,p->tel);
p++;
n++;
}
fclose(fp);
do
{
printf("\n shu ru xin ji lu hao ma:");
do
{
gets(temp.num);
}while(strcmp(temp.num,"")==0);
printf(" shu ru xin ji lu xing ming:");
gets(temp.name);
printf(" shu ru xin ji lu xing bie:");
gets(temp.sex);
printf(" shu ru xin ji lu nian ling:");
gets(temp.age);
printf(" shu ru xin ji lu xue li:");
gets("temp.xueli");
printf(" shu ru xin ji lu gong zi:");
gets(temp.gongzi);
printf(" shu ru xin ji lu zhu zi:");
gets(temp.address);
printf(" shu ru xin ji lu dian hua:");
gets(temp.tel);
for(p=record;p<record+n;p++)
if(strcmp(temp.num,p->num)==0)
{
printf("\n hao ma chong fu chong xin shu ru?(Y/N)?");
do
{
c=getch();
}while(c!='Y'&&c!='y'&&c!='N'&&c!='n');
putchar('\n');
break;
}
if(p==record+n)
{
*p=temp;
break;
}
}while(c=='Y'||c=='y');
fp=fopen("d:\\guanli.txt","r+");
if(fp==NULL)
{
printf("\n da kai wen jian shi bai!\n");
return;
}
for(p=record;p<record+n+1;p++)
fprintf(fp,"%5s%10s%5s%5s%10s%5s%10s%11s\n",p->num,p->name,p->sex,p->age,p->xueli,p->gongzi,p->address,p->tel);
fclose(fp);
}
void mydelete()
{
char c;
FILE *fp;
fp=fopen("d:\\guanli.txt","r+");
if(fp==NULL)
{
printf("\n da kai wen jian shi bai!\n");
return;
}
printf("\n an hao ma shan chu(h),an xu li shan chu(m)?");
c=getch();
if(c=='h'||c=='H')
del_num(fp);
if(c=='m'||c=='M')
del_xueli(fp);
else
printf("\n error!\n");
fclose(fp);
}
void del_num(FILE *fp)
{
int n=0;
char tempnum[10];
struct student record[N],*p,*k;
p=record;
while(feof(fp)==0)
{
fscanf(fp,"%5s%10s%5s%5s%10s%5s%10s%11s\n",p->num,p->name,p->sex,p->age,p->xueli,p->gongzi,p->address,p->tel);
p++;
n++;
}
fclose(fp);
printf("\n shu ru shan chu hao ma:");
gets(tempnum);
for(k=record;k<record+n;k++)
if(strcmp(tempnum,k->num)==0)
break;
if(k<record+n)
for(p=k;p<k+n-1;p++)
*p=*(p+1);
else
printf("\n wu chi ren!\n");
fp=fopen("d:\\guanli.txt","w");
if(fp==NULL)
{
printf("\n da kai wen jian shi bai!\n");
return;
}
for(p=record;p<record+n-1;p++)
fprintf(fp,"%5s%10s%5s%5s%10s%5s%10s%11s\n",p->num,p->name,p->sex,p->age,p->xueli,p->gongzi,p->address,p->tel);
}
void del_xueli(FILE *fp)
{
int n=0;
char tempxueli[10];
struct student record[N],*p,*k;
p=record;
while(feof(fp)==0)
{
fscanf(fp,"%5s%10s%5s%5s%10s%5s%10s%11s\n",p->num,p->name,p->sex,p->age,p->xueli,p->gongzi,p->address,p->tel);
p++;
n++;
}
fclose(fp);
printf("\n shu ru shan chu xue li:");
gets(tempxueli);
for(k=record;k<record+n;k++)
if(strcmp(tempxueli,k->xueli)==0)
break;
if(k<record+n)
for(p=k;p<k+n-1;p++)
*p=*(p+1);
else
printf("\n wu ci ren!\n");
fp=fopen("d:\\guanli.txt","w");
if(fp==NULL)
{
printf("\n da kain wen jian shi bai\n");
return;
}
for(p=record;p<record+n-1;p++)
fprintf(fp,"%5s%10s%5s%5s%10s%5s%10s%11s\n",p->num,p->name,p->sex,p->age,p->xueli,p->gongzi,p->address,p->tel);
}