路过的请看一下
谁帮我解释下,这程序文件以存储的时候就报错,我用的是VC 6.0,我想应该是二进制与ASCII之间的问题,但是我那分不清,谁跟我说一下
#include <stdio.h>
#include <stdlib.h>
#define L sizeof(struct student)
typedef struct student
{
long num;
char name[15];
struct student *next;
}stu;
stu *head,*head1;
int n=0,n1=0;
void creat()
{
stu *p1,*p2;
p1=(stu *)malloc(L);
printf("输入学号:\n");
scanf("%ld",&p1->num);
printf("输入姓名:\n");
scanf("%s",p1->name);
while(p1->num!=0)
{
system("cls");
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(stu *)malloc(L);
printf("再输入学号:\n");
scanf("%ld",&p1->num);
printf("输入姓名:\n");
scanf("%s",p1->name);
}
p2->next=NULL;
}
void cunchu( stu *p)
{
FILE *fp;
char filename[15];
system("cls");
printf("输入存储的文件名:\n");
scanf("%s",filename);
if((fp=fopen("filename","wb")==NULL))
{
printf("文件打开失败\n");
exit(0);
}
while(p->num!=0)
{
fprintf(fp,"%ld,%s",&p->num,p->name);
p=p->next;
}
}
void shuchu()
{
FILE *fp;
stu *p1,*p2;
char filename[15];
system("cls");
printf("输入所要查询信息所在的文件:\n");
scanf("%s",filename);
if((fp=fopen("filename","rb"))==NULL)
{
printf("打开失败\n");
exit(0);
}
p1=(stu *)malloc(L);
fscanf(fp,"%ld,%s",&p1->num,p1->name);
printf("学号: %ld,姓名: %s",p1->num,p1->name);
head1=NULL;
while(!feof(fp))
{
n1=n1+1;
if(n1==1)
head1=p1;
else
p2->next=p1;
p2=p1;
p1=(stu *)malloc(L);
fscanf(fp,"%ld,%s",&p1->num,p1->name);
printf("\n学号: %ld,姓名: %s",p1->num,p1->name);
}
}
void main()
{
creat();
cunchu(head);
shuchu();
system("pause");
}