结构体的使用
struct Teacher
{
int num;
char name[10];
int age;
char sex;
};
- 之后使用的时候该类型为struct Teacher(不可省略struct)
typedef struct _node
{
int value;
struct _node *pnext;
}TagNode;
读写文件操作
int Readfile(char*filename,struct User*users)
{
FILE *fp;
int i=0,res;
fp=fopen(filename,"rb");
if(fp==NULL)
{
printf("无法打开文件!\n");
exit(0);
}
while(!feof(fp))
{
res=fread(&users[i],sizeof(struct User),1,fp);
if(res==0)
continue;
i++;
}
fclose(fp);
return i;
}
struct Node* Createlist(char*filename)
{
struct Node* head,*p,*q;
FILE *fp;
int res;
struct Teacher t;
fp=fopen(filename,"rb");
if(fp==NULL)
{
printf("无法打开文件!\n");
exit(0);
}
head=(struct Node*)malloc(sizeof(struct Node));
q=head;
while(!feof(fp))
{
res=fread(&t,sizeof(struct Teacher),1,fp);
if(res==0)
continue;
p=(struct Node*)malloc(sizeof(struct Node));
q->next=p;
p->data=t;
q=p;
}
p->next=NULL;
fclose(fp);
return head;
}