标题:学生信息管理系统一直报错内存不能读
取消只看楼主
awpawp
Rank: 2
等 级:论坛游民
帖 子:15
专家分:40
注 册:2009-6-21
结帖率:100%
已结贴  问题点数:10 回复次数:2 
学生信息管理系统一直报错内存不能读
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 
struct stu
{
    int number;
    char name[20];
        int age;
    char sex[10];
   
    struct stu *next;
};
FILE *fp=NULL;
struct stu *filetostr()
{
    struct stu *pf=NULL,*pb=NULL,*head=NULL,*p=NULL;
    if((fp=fopen("student.txt","r"))==NULL)
    {
        printf("文件之前不存在!\n");
        return NULL;
    }
        pb=(struct stu *)malloc(sizeof(struct stu));
    head=pb;
        while(1)
        {
            pf=(struct stu *)malloc(sizeof(struct stu));
            if((fread(pf,sizeof(struct stu),1,fp))==1)
            {
                pb->number=pf->number;
                strcpy(pb->name,pf->name);
                pb->age=pf->age;
                strcpy(pb->sex,pf->sex);
                        pb->next=(struct stu *)malloc(sizeof(struct stu));
                p=pb;
                pb=pb->next;
               
                free(pf);
            }
            else
            {
                p->next=NULL;
                free(pb);
                break;
            }
        }
        
        
        pb=NULL;
    fclose(fp);
    return head;
}
void strtofile(struct stu *head)
{
    struct stu *pb=NULL;
    pb=head;
    if((fp=fopen("student.txt","w"))==NULL)
    {
        printf("无法打开文件!\n");
        exit(-1);
    }
    while(pb!=NULL)
    {
            fwrite(pb,sizeof(struct stu),1,fp);
        pb=pb->next;
    }
   
    fclose(fp);
    printf("保存成功!\n");
}
struct stu *add(struct stu *head)
{
    struct stu *pf=NULL,*pb=NULL,*p=NULL;
    int num;   
    printf("请输入学生信息:\n");
    printf("请输入学生的学号:");
    scanf("%d",&num);
    pf=head;
    if(head!=NULL)
    {
            pb=(struct stu *)malloc(sizeof(struct stu));
        while((pf->number<num)&&(pf->next!=NULL))
            {
                p=pf;
                pf=pf->next;
            }
        if(pf->number==num)
        {
            printf("你要添加的学生之前已经存在!\n");
            return head;
        }
        else if(pf->number>num)
        {
            if(head==pf)
            {
                head=pb;
                pb->next=pf;
                    }
            else{
                p->next=pb;
                pb->next=pf;
            }
                }
        else if(pf->next==NULL)
        {
            pf->next=pb;
            pb->next=NULL;
                }
        
            //pb=(struct stu *)malloc(sizeof(struct stu));
    }
    else
    {
        pb=(struct stu *)malloc(sizeof(struct stu));
        head=pb;
    }
    pb->number=num;
    printf("请输入学生的姓名:");
    scanf("%s",pb->name);
    printf("请输入学生的年龄:");
    scanf("%d",&pb->age);
    printf("请输入学生的性别(“man”表示男性,“woman”表示女性):");
    scanf("%s",pb->sex);
    printf("添加成功!\n");
    return head;
}
struct stu *choose(struct stu *head)
{
    struct stu *pb=NULL;
    int num;
    printf("请输入你要查找的学生的学号:");
    scanf("%d",&num);
    pb=head;
    while((pb->number!=num)&&(pb->next!=NULL))
    {
        
            pb=pb->next;
    }
    if(pb->number==num)
        return pb;
    else
        if((pb->number!=num)&&(pb->next==NULL))
        printf("你要查找的学生不存在!\n");
    return NULL;
}
struct stu *delete(struct stu *head)
{
    struct stu *pb=NULL,*pf=NULL;
    int num;
    printf("请输入你要删除的学生的学号:");
    scanf("%d",&num);
    pb=head;
    while((pb->number!=num)&&(pb->next!=NULL))
    {
        pf=pb;
        pb=pb->next;
    }
    if(pb->number==num)
    {
        if(pb==head)
            head=pb->next;
        else
            pf->next=pb->next;
            pf->next=pb->next;
        free(pb);
        printf("d删除成功!\n");
        return head;
    }
    else
        printf("你要删除的学生不存在!\n");
    return head;
}
struct stu *update(struct stu *head)
{
    struct stu *pb=NULL;
    int num;
    printf("请输入你要更改的学生的学号:");
    scanf("%d",&num);
    pb=head;
    while((pb->number!=num)&&(pb->next!=NULL))
        pb=pb->next;
    if(pb->number==num)
    {
        printf("请输入你要更改的学生的姓名:");
        scanf("%s",pb->name);
        printf("请输入修改后的学生的年龄:");
        scanf("%d",&pb->age);
        printf("请输入修改后的学生的性别(“man”表示男性,“woman”表示女性):");   
        scanf("%s",pb->sex);
        printf("更改成功!\n");
        return head;
    }
    else
        printf("你要修改的学生不存在!\n");
    return head;
}
void print(struct stu *head)
{
    struct stu *pb=NULL;
    pb=head;
    while(pb!=NULL)
    {
        printf("学号:%d    姓名:%s    年龄:%d    性别:%s\n",pb->number,pb->name,pb->age,pb->sex);
        pb=pb->next;
    }
}
void window()
{
    printf("----------------------------------------------------------------------------\n");
    printf("|**************************************************************************|\n");
    printf("|***********************学生信息管理系统***********************************|\n");
    printf("|**************************************************************************|\n");
    printf("----------------------------------------------------------------------------\n");
    printf("\n\n");
    printf("        1.添加学生信息\n");
    printf("        2.查找学生信息\n");
    printf("        3.删除学生信息\n");
    printf("        4.修改学生信息\n");
    printf("        5.打印学生信息\n");
   
    printf("        6.退出!\n");
    printf("\n\n");
}
   
int main()
{
    struct stu *head=NULL,*search_head=NULL;
    int ask;
    int saveask;
    int addask;
    printf("欢迎进入学生信息管理系统...\n");
    head=filetostr();
    while(1)
    {
        window();
        printf("请进行选择:");
        scanf("%d",&ask);
        switch(ask)
            {
                case 1:
                    head=add(head);
                    while(1)
                    {
                        printf("是否继续进行添加(0表示继续,其他数字表示离开):");
                        scanf("%d",&addask);
                        if(addask)
                            break;
                        head=add(head);
                    }
                    printf("****是否要进行保存:****\n\n");
                    printf("输入0表示不进行保存退出,其他数字表示进行保存并退出:");
                    scanf("%d",&saveask);
                    if(saveask)
                        strtofile(head);
                    break;
                case 2:
                    search_head=choose(head);
                    if(search_head!=NULL)
                    printf("学号:%d      姓名:%s                                   年龄:%d   性别:%s\n",search_head->number,search_head->name,search_head->age,search_head->sex);
                    break;
                case 3:
                    head=delete(head);
                    printf("****是否要进行保存:****\n\n");
                    printf("输入0表示不进行保存退出,其他数字表示进行保存并退出:");
                    scanf("%d",&saveask);
                    if(saveask)
                        strtofile(head);
                    break;
                case 4:
                    head=update(head);
                    printf("****是否要进行保存:****\n\n");
                    printf("输入0表示不进行保存退出,其他数字表示进行保存并退出:");
                    scanf("%d",&saveask);
                    if(saveask)
                        strtofile(head);
                    break;
                                case 5:
                    print(head);
                    break;
            case 6:
                                       exit(0);
                  
            default:
                   exit(-1);
            }   
        
        
    }


                    

搜索更多相关主题的帖子: 学生 内存 信息管理系统 
2010-04-06 16:26
awpawp
Rank: 2
等 级:论坛游民
帖 子:15
专家分:40
注 册:2009-6-21
得分:0 
提示内存不能READ,以前用的时候还可以用的,现在不知道什么原因
2010-04-06 16:27
awpawp
Rank: 2
等 级:论坛游民
帖 子:15
专家分:40
注 册:2009-6-21
得分:0 
回复 6楼 寒风中的细雨
谢谢了
2010-04-10 00:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-301874-1-1.html




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

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