欧卡2中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

需要三步,才能开始

只需两步,慢速开始

玩欧卡就用莱仕达V10方向盘欧卡2入门方向盘选莱仕达V9莱仕达折叠便携游戏方向盘支架欢迎地图Mod入驻
查看: 5758|回复: 0
收起左侧

为什么打开一个文件,会提示载入成功,却什么也不显示啊?

[复制链接]
小小女t 发表于 2012-1-7 02:54 | 显示全部楼层 |阅读模式
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
typedef struct student
{
    int num;
    int age;
    struct student *link;
}stud;

stud* creat(int n)
{
    stud *h,*p;
    int i;
    h=(stud *)malloc(sizeof(stud));
    if(h==NULL)
    {
        printf(&quot;not enough memory!&quot;);
        return(0);
    }
    h=NULL;
    for(i=0;i<=n;i++)
    {
        
        
        if((p=(struct student *)malloc(sizeof(stud)))==NULL)
            
            
            
        {
            printf(&quot;内存不足!&quot;);
            return(0);
        }
        printf(&quot;请输入数字!&quot;);
        scanf(&quot;%d&quot;,&p->num);
        scanf(&quot;%d&quot;,&p->age);
        p->link=h;
        h=p;
        
    }
    return(h);
}
  stud* insert(stud *h)
  {
      stud *p,*q,*d;
      p=h;
      int n;
      q=h->link;
      d=(stud *)malloc(sizeof(stud));
      if(d==NULL)
      {
          printf(&quot;内存不足!&quot;);
      }
      printf(&quot;请输入插入位置的前一个数!&quot;);
      scanf(&quot;%d&quot;,&n);
      

      if(n==p->num||n==p->age)
      {
      printf(&quot;请输入要插入的数 !&quot;);
      scanf(&quot;%d&quot;,&d->num);
      scanf(&quot;%d&quot;,&d->age);
      d=h;
      p=d->link;
      }
      else if
      
          (n==q->num||q->age)
      {
       printf(&quot;请输入要插入的数 !&quot;);
      scanf(&quot;%d&quot;,&d->num);
      scanf(&quot;%d&quot;,&d->age);
      d->link=p->link;
      p->link=d;
      }
      else if(n!=p->num&&n!=q->num)
      {
         while(q->num!=NULL&&p->num!=NULL)
         {
              
              
              p=p->link;
              q=q->link;
              if(n==q->num)
             {
              printf(&quot;请输入要插入的数字!&quot;);
              scanf(&quot;%d&quot;,&d->num);
              scanf(&quot;%d&quot;,&d->age);
              d->link=p->link;
              p->link=d;
               }
             break;
               
                        
         }
      }
     return(h);
  }  
  void search(stud *h)
  {
    stud *p;
    int m;
    printf(&quot;请输入需查找数据!&quot;);
    scanf(&quot;%d&quot;,&m);
    p=h;
    while(p!=NULL)
    {
   
        if(m==p->num||m==p->age)
        {printf(&quot;%d %d\n&quot;,p->num,p->age);
        
         break;
        }
        else
         p=p->link;
        
    }
         if(p==NULL)
            printf(&quot;无此数!&quot;);
   
   
   
  }
  stud *delet(stud *h)
  {
      stud *q,*p;
      p=h;
      int n;
      q=h->link;
      printf(&quot;请输入要删除的数字!&quot;);
      scanf(&quot;%d&quot;,&n);
      if(n==p->num||n==p->age)
      h=p->link;
      else if(n==q->num||n==q->age)
          p->link=q->link;
      else if(n!=p->num&&n!=q->num)
         while(q->link!=NULL&&p->link!=NULL)
         {
              
              
              p=p->link;
              q=q->link;        
             if(n==q->num||n==q->age)
              p->link=q->link;
         
         }
         
         
      return(h);
  }
void print(stud *h)
{
     stud *p;
     p=h;
     while(p!=NULL)
     {
         printf(&quot;%3d %d\n&quot;,p->num,p->age);
         p=p->link;
     }
}
void save(stud *h) //保存函数//
{
    stud *p;
    FILE *fp;
     p=h;//头指针赋给p//
    fp=fopen(&quot;d:\\wj123&quot;,&quot;a&quot;);
    if(fp==NULL)
    {
        printf(&quot;memeory error!&quot;);
    }
   
    while(p!=NULL)
    {
        fwrite(p,sizeof(stud),1,fp);
        p=p->link;
   
    fclose(fp);
    }
    printf(&quot;save successful !\n&quot;);
}
stud *load()
{
    stud *p,*h;
    FILE *fp;
    fp=fopen(&quot;d:\\wj123&quot;,&quot;a&quot;);//打开一个文件//
    if(fp==NULL)//检测文件能否打开//
    {
        printf(&quot;can not open file!&quot;);
        return(0);
    }
    p=(stud *)malloc(sizeof(stud));//申请空间。。。//

    if(p==NULL)
    {
        printf(&quot;内存溢出!&quot;);
        return(0);
    }
    h=NULL;
    while(feof(fp)!=NULL)
    {
        if(fread(p,sizeof(stud),1,fp)==NULL)//若没读到数据,跳出循环//
            break;
        else p->link=(stud *)malloc(sizeof(stud));//为下一个结点申请空间//
        if(p->link==NULL)
        {
            printf(&quot;内存溢出!&quot;);
            return(0);
        }
         p->link=h;//建立链表//
         h=p;
    }
    fclose(fp);
    printf(&quot;loading successul !\n&quot;);
    return(h);
}
void main()
{
    struct student *h;
   
   
    h=creat(1);
   
      save(h);
   
    print(h);
   
   
   
    insert(h);
    print(h);
     save(h);
   
    print(h);
    search(h);
   
      
}
大家运行下吧,只要按照提示做就好了。运行一遍后已经存在一个文件,再把load()函数放在最前面,这时候看看会是什么结果                                                                                                        <div id="post_rate_div_2021571">

联系我们|手机版|欧卡2中国 ( 湘ICP备11020288号-1 )

GMT+8, 2024-4-20 19:55 , Processed in 0.035159 second(s), 8 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表