C语言问题:大家帮我看下这个为什么出错?急!!!

[复制链接]
查看11 | 回复3 | 2011-5-22 21:37:38 | 显示全部楼层 |阅读模式
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define LEN sizeof(struct shuju)
struct shuju
{
        char name[20];
        char writer[20];
        int no;
        char press[20];
        char date[8];
        float price;
        struct shuju *next;
};
void duqu()
{
        FILE *fp;struct shuju *head2,*tail,*new1;int count=0,i,n,no;char name[20],a[20]="000",b[20]="000",writer[20],press[20];
        fp=fopen("10000.txt","r");
        for(i=1;iname);
                fscanf(fp,"%s",new1->writer);
                fscanf(fp,"%d",&new1->no);
                fscanf(fp,"%s",new1->press);
                fscanf(fp,"%s",new1->date);
                fscanf(fp,"%f",&new1->price);
                count++;
                new1->next=NULL;
                if(count==1)
                {
                        head2=new1;
                }
                else
                tail->next=new1;
                tail=new1;
        }
        fclose(fp);
        if(n==1)
        {
                printf("qing shu ru shu ming:");
                scanf("%s",name);
                do
                {
                        if(strcmp(head2->name,name)==0)
                        {
                                printf("%s%s",head2->name,head2->writer);
                                strcpy(a,head2->name);
                        }
                        else
                                head2=head2->next;
                }while(head2==NULL);
                if(a=="000")
                        printf("not founf!\n");
                strcpy(a,b);
        }
}
void main()
{
        duqu();
}
10000.txt里面内容是:bashitian zhangsan 10 beijing 19920223 20.000000 cyuyan lisi 11 nanjing 19930331 31.000000
我有一次试是成功的,能完成所想的东西,再加后面的东西就出错了。不知道怎么回事。只要这个程序能运行就可以了,不用做其他地方修改。
修改完的程序:(字数太多了,删了些)
        if((fp=fopen("10000.txt","r"))==NULL)
                printf("cannot open this file!\n");
        for(i=1;iname);
                fscanf(fp,"%s",new1->writer);
                fscanf(fp,"%d",&new1->no);
                fscanf(fp,"%s",new1->press);
                fscanf(fp,"%s",new1->date);
                fscanf(fp,"%f",&new1->price);
                count++;
                new1->next=NULL;
                if(count==1)
                {
                        head2=tail=new1;
                }
                else
                tail->next=new1;
                tail=new1;
        }
        fclose(fp);
        printf("qing shu ru yao cha xun de lei bie:");
        printf("1.shuming\n2.zuozhe\n3.shuhao\n4.chubanshe\n");
        scanf("%d",&n);
        if(n==1)
        {
                printf("qing shu ru shu ming:");
                scanf("%s",name);
                do
                {
                        if(strcmp(head2->name,name)==0)
                        {
                                printf("%s %s",head2->name,head2->writer);
                                strcpy(a,head2->name);
                        }
                        else
                                head2=head2->next;
                }while(head2!=NULL);
                if(strcmp(a,"000")==0)
                        printf("not founf!\n");
                strcpy(a,b);
        }

回复

使用道具 举报

千问 | 2011-5-22 21:37:38 | 显示全部楼层
这个程序的意图应该是将txt里面的内容读出来,组成一个链表,然后让用户输入书名进行相应的查找。如果是这样的话:#include "stdio.h"#include "stdlib.h"#include "string.h"#define LEN sizeof(struct shuju)struct shuju{char name[20];char writer[20];int no;char press[20];char date[8];float price;struct shuju *next;};void duqu(){FILE *fp;struct shuj
回复

使用道具 举报

千问 | 2011-5-22 21:37:38 | 显示全部楼层
直接出现终止,重试,忽略对话框。这种现象是内存访问出错,就是你的指针指向错误,而你却强制访问了!!自己单步运行,调试就知道了!!
回复

使用道具 举报

千问 | 2011-5-22 21:37:38 | 显示全部楼层
#include "stdio.h"#include "stdlib.h"#include "string.h"#define LEN sizeof(struct shuju)struct shuju{char name[20];char writer[20];int no;char press[20];ch
回复

使用道具 举报

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

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行