我原来是想这么实现:
void Save(STU *head)
{
FILE *fp;
STU *p;
if((fp=fopen("D:\\jilu.txt","wb"))==NULL)
{
printf("文件打开错误!!");
return;
}
p=head->next;
while(p!=NULL)
{
if(fwrite(p,sizeof(STU),1,fp)!=1)
{
printf("\nWriting failed!\n") ;
return;
}
p=p->next;
}
}
但发现文件中出现乱码,请问这是怎么回事呢?
----------------解决方案--------------------------------------------------------
全部发来看看
----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUMOFSCORE 3
#define NAMELENGTH 10
struct student
{
int num;
char *name[1];
int score[NUMOFSCORE];
float sum;
float aver;
struct student *next;
};
typedef struct student STU;
STU *firsthead;
void Appendnode(STU *head);
char Menu();
STU *FindPrenode(STU *pos);
STU *Findnode(STU *head,char give[]);
void Deletnode(STU *head);
STU *Inputrecord(STU *node);
void List(STU *head);
void Save(STU *head);
main()
{
char c;
STU *head;
head=(STU*)malloc(sizeof(STU));
head->next=NULL;
firsthead=head;
while(1)
{
c=Menu();
switch(c)
{
case '1':
List(head);
break;
case '2':
Appendnode(head);
break;
case '3':
Deletnode(head);
break;
/*case '4':
Changenode(head);
break;*/
case '5':
Save(head);
break;
case '7':
exit(0);
default:
break;
}
printf("\npress any key to main menu..");
getch();
}
}
void Save(STU *head)
{
FILE *fp;
STU *p;
if((fp=fopen("D:\\jilu.txt","wb"))==NULL)
{
printf("文件打开错误!!");
return;
}
p=head->next;
while(p!=NULL)
{
if(fwrite(p,sizeof(STU),1,fp)!=1)
{
printf("\nWriting failed!\n") ;
return;
}
p=p->next;
}
}
void List(STU *head)
{
STU *p;
p=head->next;
do{
printf("\nNO.%d\t%s%4d%4d\t%d",p->num,p->name[0],p->score[0],p->score[1],p->score[2]);
p=p->next;
}while(p!=NULL) ;
}
void Appendnode(STU *head)
{
STU *last,*newnode;
if(0==Okornot("Append"))
return;
last=head;
while(last->next!=NULL)
{
last=last->next;
}
newnode=(STU*)malloc(sizeof(STU));
newnode->next=NULL;
newnode=Inputrecord(newnode);
last->next=newnode;
}
void Deletnode(STU *head)
{
STU *linshinode;
char *name[1];
printf("\nInput name you want to delet:");
gets(name[1]);
if(0==Okornot(name))
return;
linshinode=Findnode(head,name[1]);
if(linshinode!=NULL)
{
if(linshinode==head)
{
firsthead=linshinode->next;
head->next=linshinode->next;
}
if(linshinode->next==NULL)
{
FindPrenode(linshinode)->next=NULL;
}
else
FindPrenode(linshinode)->next=linshinode->next;
}
else
{
printf("\n输入有误!!") ;
}
}
STU *Findnode(STU *head,char give[])
{
STU *p;
p=head;
while(p->next!=NULL)
{
if(strcmp(p->name[0],give)==0)
{
return p;
}
p=p->next;
}
return NULL;
}
STU *FindPrenode(STU *pos)
{
STU *p;
p=firsthead;
while(p->next!=NULL)
{
if(p->next==pos)
{
return p;
}
p=p->next;
}
return NULL;
}
/*int Getnodenum(STU *head);
{
int i=0;
while(1)
{
}
}*/
STU *Inputrecord(STU *node)
{
int i;
printf("\nnumber of student:");
scanf("%d",&(node->num));
printf("\nNO.%d's name:",node->num);
scanf("%s",node->name[0]);
for(i=0;i<NUMOFSCORE;i++)
{
printf("\n第%d门课成绩:",i+1);
scanf("%d",&(node->score[i]));
}
for(node->sum=0,i=0;i<NUMOFSCORE;i++)
{
node->sum=node->score[i]+node->sum;
}
node->aver=node->sum/NUMOFSCORE;
return node;
}
int Okornot(char *name)
{
char c;
printf("Do you really want to %s",name);
c=getch();
if(c=='y'||c=='Y')
return 1;
else
return 0;
}
char Menu()
{
char c;
printf("\n1. List Record.");
printf("\n2. Append record");
printf("\n3. Delet record");
printf("\n4. Change record.");
printf("\n5. Save.");
printf("\n6. Sort record according to sum.");
printf("\n7. Exit to DOS.");
c=getch();
return c;
}
----------------解决方案--------------------------------------------------------
这是一个未完成的学生成绩管理
----------------解决方案--------------------------------------------------------