| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 490 人关注过本帖
标题:没有语法错误,但运行程序是出现内存错误!!链表的创建输出和文件的读写问 ...
只看楼主 加入收藏
寂寞编程孤独
Rank: 1
等 级:新手上路
帖 子:20
专家分:6
注 册:2010-10-26
结帖率:57.14%
收藏
已结贴  问题点数:16 回复次数:1 
没有语法错误,但运行程序是出现内存错误!!链表的创建输出和文件的读写问题!!
#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
这是上面程序读取的文件中的数据!!
搜索更多相关主题的帖子: 学生成绩 结构体 桌面 新建 
2011-01-24 18:23
广陵绝唱
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:29
帖 子:3607
专家分:1709
注 册:2008-2-15
收藏
得分:16 
帮你改了一下,你试试看看:
程序代码:
#include<stdio.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 *p,*pr;
    //int i=1;
    p=pr=head=(struct student *)malloc(sizeof(struct student));
    //end=(struct student *)malloc(sizeof(struct student));
    p->p_next=NULL;
    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(fscanf(fp1,"%d%f%f",&p->number,&p->english,&p->math)!=EOF)
    {
        p=(struct student *)malloc(sizeof(struct student));
        p->p_next=NULL;
        pr->p_next=p;
        pr=p;
        //fp1=fopen("d:\\a.txt","r");
        //fscanf(fp1,"%d%f%f",&p->number,&p->english,&p->math);
        //fscanf(fp1,"%d%f%f",&end->number,&end->english,&end->math);
    }
    free(p);
    return(head);           /*返回链表的头地址*/
}


void print(struct student *head)        /*将链表输出的函数*/
{
    while(head->p_next!=NULL)
    {
        printf("NO. %d : english= %f  math= %f\n",head->number,head->english,head->math);
        head=head->p_next;
    }
}
int main(void)
{
    //struct student *create();           /*创建链表*/
    //void print(struct student *head);   /*输出链表*/
    struct student *p;         /*在链表中搜索number,并将对应于number的成绩输出到文件中*/
    //int j=0;
    p=head=create();
    /*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);*/
    print(head);

    return 0;
}

2011-01-25 13:02
快速回复:没有语法错误,但运行程序是出现内存错误!!链表的创建输出和文件的读 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018809 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved