#include<stdio.h>
#include<malloc.h>
struct student
{
int num;
char name;
int age;
int sex;
struct student *link;
}stud;
creat(int n)
{
struct student *h,*p;
int i;
h=(struct student *)malloc(sizeof(stud));
if(h==NULL)
{
printf("not enough memory!");
return(0);
}
h=NULL;
for(i=0;i<=n;i++)
{
if((p=(struct student *)malloc(sizeof(stud)))==NULL)
{
printf("内存不足!");
return(0);
}
scanf("%d",&p->num);
scanf("%s",&p->name);
scanf("%d",&p->age);
scanf("%s",&p->sex);
p->link=h;
h=p;
}
}
void save(struct student *h)
{
FILE *fp;
struct student *p;
if((fp=fopen("d:\\123","w"))==NULL)
{
printf("can now open file!");
}
printf("\n saving file...\n");
p=h;
while(p!=NULL)
{
fwrite(&p,sizeof(stud),1,fp);
p=p->link;
}
fclose(fp);
printf("*******save successful!********\n");
}
void main()
{
struct student *h;
h=(struct student *)creat(4);
save(h);
}
编译通过了,为什么存储时会出现。。不能为read呢?
#include<malloc.h>
struct student
{
int num;
char name;
int age;
int sex;
struct student *link;
}stud;
creat(int n)
{
struct student *h,*p;
int i;
h=(struct student *)malloc(sizeof(stud));
if(h==NULL)
{
printf("not enough memory!");
return(0);
}
h=NULL;
for(i=0;i<=n;i++)
{
if((p=(struct student *)malloc(sizeof(stud)))==NULL)
{
printf("内存不足!");
return(0);
}
scanf("%d",&p->num);
scanf("%s",&p->name);
scanf("%d",&p->age);
scanf("%s",&p->sex);
p->link=h;
h=p;
}
}
void save(struct student *h)
{
FILE *fp;
struct student *p;
if((fp=fopen("d:\\123","w"))==NULL)
{
printf("can now open file!");
}
printf("\n saving file...\n");
p=h;
while(p!=NULL)
{
fwrite(&p,sizeof(stud),1,fp);
p=p->link;
}
fclose(fp);
printf("*******save successful!********\n");
}
void main()
{
struct student *h;
h=(struct student *)creat(4);
save(h);
}
编译通过了,为什么存储时会出现。。不能为read呢?