QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 742|回复: 5

帮我看看程序错在哪.

[复制链接]
发表于 2005-6-1 16:39:47 | 显示全部楼层 |阅读模式
uname -a
Linux nxgzserver 2.4.21-4.ELsmp #1 SMP Fri Oct 3 17:52:56 EDT 2003 i686 i686 i386 GNU/Linux

cc 1.c
1.c: In function `save2file':
1.c:64: warning: assignment makes pointer from integer without a cast

cat 1.c
#include <stdio.h>
#include <malloc.h>
#include <string.h>

struct fapiao{
  char fp[30];
  struct fapiao *next;
};
typedef struct fapiao FAPIAO;

main()
{
char fpstr[30];
FAPIAO *head;

void insert(FAPIAO **,char *);
void save2file(FAPIAO **,char *);

head=NULL;
while (1)
{
   printf("Please enter the fapiao serial number,exit program with string \"exit\":\n");
   scanf("%s",fpstr);
   if (strcmp(fpstr,"exit")==0) break;
   insert(&head,fpstr);
}
printf("Please enter the file name for save datas:\n");
scanf("%s",fpstr);
save2file(&head,fpstr);
}

void insert(FAPIAO **head,char *string)
{
  FAPIAO *cur,*pre,*new;
   
  if ((new=(FAPIAO *)malloc(sizeof(FAPIAO)))==NULL)
     {printf("Can't creat new node!\n");return;}
  strcpy(new->fp,string);
  new->next=NULL;
  pre=*head;
  if (pre==NULL)
     *head=new;
  else
     {cur=pre;
      while(cur!=NULL)
         {if(strcmp(string,cur->fp)==0)
             {printf("Repeated!\n");return;}
          pre=cur;
          cur=cur->next;
         }
      pre->next=new;
      return;
     }
}

void save2file(FAPIAO **head,char *string)
{
FAPIAO *cur,*pre;
FILE *fptr;

pre=*head;
if (pre==NULL) return;
cur=pre;
if (fptr=fopen(string,"w")==NULL)
   {printf("Can't creat file!\n");return;}
while (cur!=NULL)
  {fprintf(fptr,"%30s\n",cur->fp);
     *head=cur;
     cur=cur->next;
     free(pre);
     pre=*head;
  }

fclose(fptr);
return;
}
 楼主| 发表于 2005-6-1 16:41:04 | 显示全部楼层
cc 1.c
1.c: In function `save2file':
1.c:64: warning: assignment makes pointer from integer without a cast
ls
1~  1.c  1.c~  1.sh  1.sh~  a.out  bin  Desktop  first  mv  string  study  公证登记表
./a.out
Please enter the fapiao serial number,exit program with string "exit":
1
Please enter the fapiao serial number,exit program with string "exit":
2
Please enter the fapiao serial number,exit program with string "exit":
3
Please enter the fapiao serial number,exit program with string "exit":
4
Please enter the fapiao serial number,exit program with string "exit":
3
Repeated!
Please enter the fapiao serial number,exit program with string "exit":
exit
Please enter the file name for save datas:
ttt
段错误
[root--680--~]ls
1~  1.c  1.c~  1.sh  1.sh~  a.out  bin  Desktop  first  mv  string  study  ttt  公证登记表
[root--681--~]cat ttt
[root--682--~]
ttt为空
回复

使用道具 举报

发表于 2005-6-2 12:11:13 | 显示全部楼层
if (fptr=fopen(string,"w")==NULL)
应该是
if (fptr=fopen(string,"w")==0)
而且,这种写法是很不好的习惯
回复

使用道具 举报

发表于 2005-6-2 12:20:05 | 显示全部楼层
void save2file(FAPIAO **head,char *string)
{
FAPIAO *cur,*pre;
FILE *fptr;

pre=*head;
if (pre==NULL) return;
cur=pre;
if (fptr=fopen(string,"w")==NULL)
{printf("Can't creat file!\n");return;}
while (cur!=NULL)
{fprintf(fptr,"%30s\n",cur->fp);
/*   *head=cur;  */
cur=cur->next;
free(pre);
/*   pre=*head;  */
pre=cur;
}
回复

使用道具 举报

发表于 2005-6-2 12:22:58 | 显示全部楼层
不过哪里出错,我也不确定
呵呵
回复

使用道具 举报

发表于 2005-6-2 17:01:40 | 显示全部楼层
我看是优先级的问题:
第64行改为:
if ((fptr=fopen(string,"w"))==NULL)
PS:
new在c++里是关键字,不要用它作变量名.

typedef struct fapiao FAPIAO;
这种风格我以前也用过,不过现在用这种了:
typedef struct fapiao fapiao_t;
typedef fapiao_t* p_fapiao_t;

void类型的函数往往不是很好的选择,试一下int来代替,成功是返回0,出错时返回-1,并设置出错信息.

而且把main函数放最后是不错的选择!^_^
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-5 21:43 , Processed in 0.043654 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

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