| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 975 人关注过本帖
标题:学生信息管理系统一直报错内存不能读
只看楼主 加入收藏
awpawp
Rank: 2
等 级:论坛游民
帖 子:15
专家分:40
注 册:2009-6-21
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:9 
学生信息管理系统一直报错内存不能读
#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
BINGQING
Rank: 2
来 自:武汉
等 级:论坛游民
帖 子:98
专家分:75
注 册:2010-3-29
收藏
得分:0 
我的VC能运行啊。不过我也遇到过类似的情况,也不知道怎样解决。
2010-04-06 21:36
一口三个汉堡
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:155
专家分:525
注 册:2010-3-21
收藏
得分:0 
我在Vc上可以运行啊,你把VC重装下好了,VC经常出现这种情况

坚持做对的事情,而不是容易的事情。
2010-04-06 21:44
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
系统还得加工  没有进行错误输入处理的功能
2010-04-06 22:06
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:10 
#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) )
        {
            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)
{    //加判断语句
    if( !head )
        return;
    struct stu *pb=NULL;
    pb=head;
    if((fp=fopen("student.txt","w"))==NULL)
    {
        printf("无法打开文件!\n");
        exit(-1);
    }
    while(pb)
    {
        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;
        }*/
        while( pf &&  (pf->number<num))//表示过滤掉比这个学号小的人
        {
            p = pf;//p 指向的是比num 小的学号 或 NULL
            pf = pf->next;
        }//pf 出来指向的是不比num小的学号的结点 或 最后一个结点

        if(pf->number==num)//pf当前指向相等的情况
        {
            printf("你要添加的学生之前已经存在!\n");
            return head;
        }
        else if(pf->number>num)//pf当前指向大于num的情况
        {
            if(head==pf)//第一个结点就大于num
            {
                /*head=pb;
                pb->next=pf;*/
                pb->next = head;
                head = pb;//保证head总是指向第一个结点
            }
            else
            {
                p->next = pb;
                pb->next = pf;
            }
        }
        else if( pf == NULL )//pf当前指向最后一个结点
        {
            /*pf->next = pb;
            pb->next=NULL;*/
            pb->next = pf;
            pf = pb;
        }     
            //pb=(struct stu *)malloc(sizeof(struct stu));
    }
    else//第一次的插入
    {
        pb = (struct stu *)malloc(sizeof(struct stu));
        head = pb;
        pb->next = NULL;//进行未结点的处理
    }
    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)
{
    //加判断语句
    if( !head )
    {
        printf("未存有学生信息!\n");
        return NULL;
    }
    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)
{
    //加判断语句
    if( !head )
    {
        printf("未存有学生信息!\n");
        return NULL;
    }

    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;
    }*/
    while( pb && (pb->number!=num) )//可以一直找到最后一个结点即 可以全部察寻一遍
    {
        pf = pb;
        pb = pb->next;

    }//假如找到 则pb就是要删除的 或则 就没有找到
    //if(pb->number==num)
    if( pb )//表示pb不为NULL 即 查询到了
    {
        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)
{
    //加判断语句
    if( !head )
    {
        printf("未存有学生信息!\n");
        return NULL;
    }
    struct stu *pb=NULL;
    int num;
    printf("请输入你要更改的学生的学号:");
    scanf("%d",&num);
    pb=head;
    /*while((pb->number!=num)&&(pb->next!=NULL))
        pb=pb->next;*/
    while( pb && (pb->number!=num) )//如果pb非NULL 指向要修改的结点
        pb=pb->next;
    if( pb )
    {
        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 )
    {
        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);
            }
            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);
            break;
        case 4:
            head=Update(head);
            break;
        case 5:
            Print(head);
            break;
        case 6:
            printf("****是否要进行保存:****\n\n");
            printf("输入0表示不进行保存退出,其他数字表示进行保存并退出:");
            scanf("%d",&saveask);
            if(saveask)
                Strtofile(head);

            exit(0);                  
        default:
                exit(-1);
        }            
    }
    return 0;
}
2010-04-07 07:41
cl165
Rank: 2
等 级:论坛游民
帖 子:7
专家分:17
注 册:2010-3-25
收藏
得分:0 
楼上正解
2010-04-07 10:39
洁jay
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-4-5
收藏
得分:0 
unexpected end of file while looking for precompiled header directive
我运行这个程序时出的问题。请问这是怎么了??
我大括号和分号都正确啊!!
2010-04-07 12:28
awpawp
Rank: 2
等 级:论坛游民
帖 子:15
专家分:40
注 册:2009-6-21
收藏
得分:0 
回复 6楼 寒风中的细雨
谢谢了
2010-04-10 00:48
cokelm
Rank: 1
来 自:湖南
等 级:新手上路
帖 子:11
专家分:0
注 册:2010-4-2
收藏
得分:0 
不乏高手!
2010-04-10 11:11
快速回复:学生信息管理系统一直报错内存不能读
数据加载中...
 
   



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

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