没有语法错误,但运行程序是出现内存错误!!链表的创建输出和文件的读写问题!!
#include"stdafx.h"#include"stdlib.h"
struct student /* 学生成绩结构体*/
{
int number;
float english;
float math;
struct student *p_next;
};
FILE *fp1,*fp2;
struct student *head;
struct student *create() /*创建学生成绩链表的函数*/
{
struct student *end,*NEW;
int i=1;
head=(struct student *)malloc(sizeof(struct student));
end=(struct student *)malloc(sizeof(struct student));
fp1=fopen("C:\\Documents and Settings\\Administrator\\桌面\\新建 文本文档","r");
fscanf(fp1,"%d%f%f",&head->number,&head->english,&head->math); /*从文件中读取数据*/
fscanf(fp1,"%d%f%f",&end->number,&end->english,&end->math);
while(&end->number!=NULL)
{
NEW=(struct student *)malloc(sizeof(struct student));
NEW->p_next=NULL;
end->p_next=NEW;
end=NEW;
fp1=fopen("C:\\Documents and Settings\\Administrator\\桌面\\新建 文本文档","r");
fscanf(fp1,"%d%f%f",&NEW->number,&NEW->english,&NEW->math);
fscanf(fp1,"%d%f%f",&end->number,&end->english,&end->math);
}
free(NEW);
return(head); /*返回链表的头地址*/
}
void print(struct student *head) /*将链表输出的函数*/
{
while(head->p_next!=NULL)
{
printf("%d%f%f",head->number,head->english,head->math);
head=head->p_next;
}
}
void main()
{
struct student *create(); /*创建链表*/
void print(struct student *head); /*输出链表*/
struct student *p; /*在链表中搜索number,并将对应于number的成绩输出到文件中*/
int j=0;
p=head;
while(p->number!=3)
{
head=head->p_next;
p=head;
j++;
}
fp2=fopen("C:\\Documents and Settings\\Administrator\\桌面\\新建 文本文档2","w");
fprintf(fp2,"%d%f%f",p->number,p->english,p->math);
}
高手指点一下!!
1 80 90
2 85 91
3 78 79
4 69 84
这是上面程序读取的文件中的数据!!