首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

哪位牛人帮小弟我看看

2012-10-21 
哪位牛人帮我看看#include iostream#include stdio.h#include stdlib.h#include string.husing na

哪位牛人帮我看看
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

struct node
{
  char dat[4];
  int num;
  struct node *next;
};

struct node *getnode()
{
  struct node *p;
  int i;
  p=(struct node*)malloc(sizeof(struct node));
  for(i=0;i<4;i++) p->dat[i]='\0';
  p->num=0;
  p->next=NULL;
  return p;
}

void creatdate()
{
  struct node *head,*p;
  FILE *fd;
  char buf[4]="123";
  int i;
  head=getnode();
  if((fd=fopen("./learndate","r"))==NULL) cout<<"cannot open file:learndate!"<<endl;
  while(!feof(fd))
  {
  for(i=0;i<4;i++) buf[i]='\0';
  fread(buf,3,1,fd);
  for(p=head;p!=NULL;p=p->next)
  {printf("ceshis : %s %s %d\n",buf,p->dat,p->num);
  if(0==strcmp(p->dat,buf)) p->num++;
  }
  if(NULL==p)
  {
  p->next=getnode();
  p=p->next;
  strcpy(p->dat,buf);
  p->dat[3]='\0';
  }
  fseek(fd,-2,1);
  }
  fclose(fd);
}
编译没有问题,运行时总是出错,编译器是 Mingw32

[解决办法]

C/C++ code
if(NULL==p) 这个地方有问题,既然P指向了NULL,那么就没有next了。所以if里的第一句就不成立,会出现段错误。  {  p->next=getnode();  p=p->next;  strcpy(p->dat,buf);  p->dat[3]='\0';  } 

热点排行