typedef 结构体 怎么写文件
不会读写 求指教#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <windows.h>
#include<ctype.h>
#include<string.h>
#define CRNUM 200
#define STNUM 40
#define TERMS 4
void sjbc();
void jm();
void zcxt();
struct zcxx
{char zh[20];
char mm[10];
int gs;
int kk;
int pp;
char wjm[20];
}gr;
void zcxt()
{struct zcxx gr;char username[20];int j=0;
FILE *fp;
zc: if((fp=fopen("c:/zcxt.txt","a+"))==NULL)
{printf("打开文件失败\n");
exit(1);}
do{fflush(stdin);
printf("请输入注测的账号:");
gets(gr.zh);
strcpy(username,gr.zh);
rewind(fp);
while(!feof(fp))
{if(fread(&gr,sizeof(struct zcxx),1,fp)>0)
{if (strcmp(gr.zh,username)==0)
{printf("此账号已被注册请重新输入\n");fclose(fp);goto zc;}}
}
strcpy(gr.zh,username);
printf("\n请输入注测的密码:");gets(gr.mm);
gr.kk=0;
gr.pp=0;
gr.gs=1;
printf("请输入文件名为避免重返请\n");
gets(gr.wjm);
rewind(fp);
fwrite(&gr,sizeof(struct zcxx),1,fp);}while(0);
fclose(fp);
}
typedef
struct message
{ int num,grade,mcode;
struct message *ll,*rl,*next;
}
MPOINT;
MPOINT *mp;
typedef
struct coursechain
{ int ccode,cterm;
MPOINT *mp;
struct coursechain *next;
}
CPOINT;
CPOINT *cp;
typedef
struct directory
{ int dclass;
CPOINT *cp;
struct student
{ char dname[20];
MPOINT *mp[TERMS];
} stu[STNUM];
struct directory *next;
}
DPOINT;
void jm()
{CPOINT *cp;
MPOINT *mp;DPOINT *dp;
system("cls");
int i,j=1,k=0,w=0;
char t;
FILE *fpl;
printf("是否要打开数据文件Y/N %d\n",gr.gs);
scanf("%c",&t);
getchar();
while(t!='Y'&&t!='y'&&t!='N'&&t!='n')
{ printf("请输入Y键或N键:");
scanf("%c",&t);
getchar();
}
if(t=='Y'||t=='y')
{if((fpl=fopen(gr.wjm,"rt+"))==NULL)
{printf("打开文件失败 没有数据\n");
exit(1);}dp=(DPOINT*)malloc(sizeof(DPOINT));
st:
cp=dp->cp;
mp=cp->mp;
fread(dp,sizeof(DPOINT),1,fpl);
fread(cp,sizeof(CPOINT),1,fpl);
printf("255551\n");
fread(mp,sizeof(MPOINT),1,fpl);
dp->next=dphead;
dphead=dp;j=j+1;
while(j<=gr.gs)
{dp=dp->next;
goto st;}
fclose(fpl);}
for(i=0;i<CRNUM;i++)
coc[i][0]=nulnam;
do
{ printf("\n");
printf(" _____________________________________________\n");
printf(" 学生成绩管理系统功能表 \n");
printf(" ============================================= \n");
printf(" >> 1. 班级、学生登记注册 \n");
printf(" >> 2. 学生成绩登记 \n");
printf(" >> 3. 学生成绩修改 \n");
printf(" >> 4. 打印各班学生单科成绩 \n");
printf(" >> 5. 打印各班学生一学期各科平均成绩 \n");
printf(" >> 6. 打印各科指定分数段内的学生名单 \n");
printf(" >> 7. 数据保存 \n");
printf(" >> 0. 结束程序运行 \n");
printf("\n");
printf("请输入您选择的功能号(0~7):");
scanf("%d",&i);
getchar();
if(i>0)
switch(i)
{
case 1: registerstudent(); break;
case 2: markregister(); break;
case 3: modify(); break;
case 4: printreport(); break;
case 5: countaverage(); break;
case 6: segmentprint(); break;
case 7: sjbc(); break;
};
printf("\n");system("cls");
}
while(i>0);
}
main()
{ system("color 9");
dphead=NULL;
int h,i=0;FILE *fp;
char username[20],pwd[10];
if((fp=fopen("c:/zcxt.txt","a+"))==NULL)
{printf("打开文件失败\n");
exit(1);}
do{printf("\n 登陆请按:1\n 注册请按:2\n");
scanf("%d",&h);}while(h<1||h>2);
if(h==1)
{st:
printf("\n 请输入账号:");
fflush(stdin); gets(username);
printf("\n 请输入密码:");
gets(pwd);
rewind(fp);
while(!feof(fp))
{if(fread(&gr,sizeof(struct zcxx),1,fp)>0)
{if (strcmp(gr.zh,username)==0&&strcmp(gr.mm,pwd)==0)
{i=1;jm();}}
}
}
if(h==2)
{zcxt();h=3;
printf("\n 请输入账号:");
fflush(stdin); gets(username);
printf("\n 请输入密码:");
gets(pwd);
rewind(fp);
while(!feof(fp))
{if(fread(&gr,sizeof(struct zcxx),1,fp)>0)
{if (strcmp(gr.zh,username)==0&&strcmp(gr.mm,pwd)==0)
{fclose(fp);i=1;jm();}}
};
}
if(i==0){printf("你输入的账号或密码有误!!请重新输入");fclose(fp);if((fp=fopen("c:/zcxt.txt","a+"))==NULL)
{printf("打开文件失败\n");
exit(1);} goto st;}
fclose(fp);
}
DPOINT *searchdp(int class)
{ DPOINT *dp;
dp=dphead;
while(dp!=NULL&&dp->dclass!=class)
dp=dp->next;
return dp;
}
void sjbc()
{DPOINT *dp;int n=0,i=1,j=0;
dp=dphead;
while(dp!=NULL)
{n++;
dp=dp->next;
}
char username[20];
FILE *fp; FILE *fpl;
if((fp=fopen("c:/zcxt.txt","rt+"))==NULL)
{printf("打开文件失败\n");
exit(1);}
strcpy(username,gr.zh);
while(!feof(fp))
{if(fread(&gr,sizeof(struct zcxx),1,fp)>0)
{if (strcmp(gr.zh,username)==0)break;}
}
fseek(fp,-sizeof(struct zcxx),1);
gr.gs=n;
fwrite(&gr,sizeof(struct zcxx),1,fp);fclose(fp);
if((fpl=fopen(gr.wjm,"wt+"))==NULL)
{printf("打开文件失败\n");
exit(1);} fclose(fpl);
printf("文件已创建完毕\n");
CPOINT *cp;
MPOINT *mp;
if((fpl=fopen(gr.wjm,"rt+"))==NULL)
{printf("打开文件失败\n");
exit(1);}
dp=dphead;
sp:
cp=dp->cp;mp=cp->mp;
fwrite(dp,sizeof(DPOINT),1,fpl);
for(j=1;cp!=NULL;cp=cp->next,j++)
fwrite(cp,sizeof(CPOINT),1,fpl);
gr.kk=j;
printf("%d ",j);
for(j=1;mp!=NULL;mp=mp->next,j++)
fwrite(mp,sizeof(MPOINT),1,fpl);
gr.pp=j;
printf("%d ",j);
dp=dp->next;i=i+1;
while(i<=n)
goto sp;
fclose(fpl);
printf("文件已保存\n");
getchar();
getchar();
}