求大神!~没有一点错,却输出不了文件里的信息。。。
#include <stdio.h>#include <stdlib.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
#define LEN sizeof(struct student)
#define FORMAT "%-10d%-15s%-12.1ls%-12.1ls%-12.11s%\n"
#define DATA stu[i].num,stu[i].name,stu[i].shijian,stu[i].liyou,stu[i].xuke
void menu();
struct student
{
int num;
char name[15];
char shijian;
char liyou;
char xuke;
};
struct student stu[50];
void chakan();
void qingjia();
int main()
{
int n;
menu();
scanf("%d",&n);
while(n)
{
switch(n){
case 1:qingjia();
break;
case 3:chakan();
break;
default:
break;
}
getch();
menu();
scanf("%d",&n);
}
return 0;
}
void menu()
{
printf("\n\n\n\n\n");
printf("\t\t|---------------------STUDENT------------------------|\n");
printf("\t\t|\t 0. exit |\n");
printf("\t\t|\t 1. Student Interface |\n");
printf("\t\t|\t 2. Administrator Interface |\n");
printf("\t\t|\t 3. Teacher Interface |\n");
printf("\t\t|----------------------------------------------------|\n\n");
printf("\t\t\tchoose(0-3):");
}
void qingjia()
{
int i,m=0;
char ch[20];
FILE *fp;
if((fp=fopen("data.txt","a+"))==NULL)
{
printf("不能打开\n");
return;
}
while(!feof(fp)) {
if(fread(&stu[m] ,LEN,1,fp)==1)
m++;
}
fclose(fp);
if(m==0)
printf("没有记录!\n");
else
{
system("cls");
chakan();
}
if((fp=fopen("data.txt","wb"))==NULL)
{ printf("不能打开\n");
return;}
for(i=0;i<m;i++)
{
fwrite(&stu[i] ,LEN,1,fp);
}
printf("请输入(y/n):");
scanf("%s",ch);
while(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)
{
printf("请输入学号:");
scanf("%d",&stu[m].num);/*输入学生学号*/
for(i=0;i<m;i++)
if(stu[i].num==stu[m].num)
{
printf("此学号已存在,按任意键继续!");
getch();
fclose(fp);
return;
}
printf("姓名:");
scanf("%s",stu[m].name);
getchar();
printf("请假理由:");
scanf("%s",&stu[m].liyou);
getchar();
printf("请假时间:");
scanf("%s",&stu[m].shijian);
getchar();
printf("是否许可:");
scanf("%s",&stu[m].xuke);
getchar();
if(fwrite(&stu[m],LEN,1,fp)!=1)/*将新录入的信息写入指定的磁盘文件*/
{ printf("不能保存!"); getch(); }
else { printf("%s 已保存!\n",stu[m].name);m++;}
printf("是否继续?(y/n):");/*询问是否继续*/
scanf("%s",ch);
}
fclose(fp);
printf("OK!\n");
}
void chakan()
{ FILE *fp;
int i,m=0;
fp=fopen("data.txt","rb");
while(!feof(fp))
{
if(fread(&stu[m] ,LEN,1,fp)==1)
m++;
}
fclose(fp);
printf("number name liyou shijian xuke \t\n");
for(i=0;i<m;i++)
{
printf(FORMAT,DATA);//
}
}