| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 718 人关注过本帖
标题:链表保存问题 麻烦各高手帮忙解决
只看楼主 加入收藏
eed
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-9-8
收藏
 问题点数:0 回复次数:3 
链表保存问题 麻烦各高手帮忙解决
我先申了全局的:
struct Stdinfo
{
  char num[MAX];  /* for students number */
  char names[MAX];  /* for students name */
  float score;
  struct Stdinfo *nextAddr;
};

/* here is the definition of the first structure pointer */
struct Stdinfo *tosp;


下面两个是我输入链表内容的函数:
void add_record(struct Stdinfo *p)
{
  char names[MAX];
  char num[MAX];
  float score;
  void push(struct Stdinfo *,char *,char *,float);

  getchar();
  printf("\n--------------------------------------------------------------------------------\n");
  printf("\nEnter as many information as you wish.\n");
  while (1)
  {
    printf("\nPlease enter the student's number:");
    printf("\n(To stop entering,enter a single x)");
    scanf("%s",num);
    getchar();
    if (strcmp(num,"x")==0)
      break;
    printf("Please enter the student's name:");
    gets(names);
    printf("Please enter the student's score:");
    scanf("%f",&score);
    getchar();
   
    printf("\nNow you have input the imformation of this student successful.\n");
    printf("--------------------------------------------------------------------------------\n");
    push(tosp,num,names,score);
  }
}

void push(struct Stdinfo *p,char *num,char *names,float score)
{
  struct Stdinfo *newaddr;
  newaddr=(struct Stdinfo *) malloc(sizeof(struct Stdinfo));
  if (newaddr==(struct Stdinfo *) NULL)
  {
    printf("\nFailed to allocate memory for this structure.\n");
    exit(1);
  }
  strcpy(newaddr->num,num);
  strcpy(newaddr->names,names);
  newaddr->score=score;
  newaddr->nextAddr=p;
  tosp=newaddr;
}
上面两个是我输入链表内容的函数

下面是我保存链表的函数:
void save_record(struct Stdinfo *tosp)
{
  FILE *infile;

  infile=fopen("c:\\record.txt","w");   /* to record the information in the "record.txt" file */
  if(infile==NULL)
    {
      printf("\nFailed to open the file.\n");
      exit(1);
    }
    while(tosp!=NULL)
    {
      fwrite(tosp,sizeof(struct Stdinfo),1,infile);
      tosp=tosp->nextAddr;
    }
    fclose(infile);
}

现在是以保存就出现Null pointer assignment,且少存了最后输入的一组数据。
搜索更多相关主题的帖子: 链表 麻烦 保存 
2008-09-09 10:19
heartstring
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2008-9-9
收藏
得分:0 
push 函数有问题,你没有考虑表头表尾问题
2008-09-09 11:13
eed
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-9-8
收藏
得分:0 
我在main函数里写了tosp=NULL;
2008-09-09 12:30
heartstring
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2008-9-9
收藏
得分:0 
[bo][un]eed[/un] 在 2008-9-9 12:30 的发言:[/bo]

我在main函数里写了tosp=NULL;

nextAddr关键是这个
2008-09-09 12:50
快速回复:链表保存问题 麻烦各高手帮忙解决
数据加载中...
 
   



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

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