在结构体链表的结尾增加一个节点,再输出,为什么最后一个节点的输出会是乱码?


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n=5;//定义全局变量记录系统图书总量
typedef struct book
{
    int ISBN;//图书编号
    char name[30];//书名
    char writer[30];//作者
    char publisher[30];//出版社
    char time[30];//出版日期
    int price;//价格
    struct book *next;
}book;

book *create()
{
    book *head=NULL,*p=NULL,*q=NULL;
    FILE *fq;
    fq=fopen("book.txt","r");
    int i=0;
    while(i<=n)
    {
        p=(book*)malloc(sizeof(book));
        fscanf(fq,"%d%s%s%s%s%d",&p->ISBN,p->name,p->writer,p->publisher,p->time,&p->price);
        i++;
        p->next=NULL;
        if(i==1)
            head=p;
        else
            q->next=p;
            q=p;
    }
    fclose(fq);
    return head;
}//将已知文件中的各图书信息存入一个新建的链表
void print(book *head)
{
    book *p=NULL,*q=NULL;
    p=head;
    while(p->next!=NULL)
    {
        printf("ISBN号:%d 书名:%s 作者:%s 出版社:%s 出版时间:%s 价格:%d\n",p->ISBN,p->name,p->writer,p->publisher,p->time,p->price);
        p=p->next;
    }
}
void add()
{
    book *h=NULL;
    FILE *fp;
    fp=fopen("book.txt","a+");
    int a,b,x;
    char c[30],d[30],e[30],f[30],k=' ';
    printf("请输入图书ISBN号:");
    scanf("%d",&a);
    fprintf(fp,"\n%d ",a);
    printf("请输入书名:");
    scanf("%s",c);
    fputs(c,fp);
    fputc(k,fp);
    printf("请输入作者:");
    scanf("%s",d);
    fputs(d,fp);
    fputc(k,fp);
    printf("请输入出版社:");
    scanf("%s",e);
    fputs(e,fp);
    fputc(k,fp);
    printf("请输入出版时间:");
    scanf("%s",f);
    fputs(f,fp);
    printf("请输入价格:");
    scanf("%d",&b);
    fprintf(fp," %d",b);
    printf("请选择下一步操作;\n");
    printf("1.继续录入图书信息  0.查看当前图书情况\n");
    scanf("%d",&x);
    n++;
    if(x==0)
        {h=create();
        print(h);}
    if(x==1)
        add();
    fclose(fp);
}//录入图书信息

int main()
{
    book *h,*p;
    int a;
    printf("欢迎使用图书登记管理程序\n");
    printf("请选择您的功能序号\n");
    printf("1.录入图书信息  2.查找图书信息\n");
    printf("3.修改图书信息  4.删除图书信息\n");
    printf("5.根据书名查找图书\n");
    printf("6.根据出版社查找图书\n");
    printf("7.统计出版社出版的图书数量\n");
    printf("8.浏览系统图书总量\n");
    scanf("%d",&a);
    switch(a)
    {
        case 1:add();break;
        /*case 2:head=;break;
        case 3:head=;break;
        case 4:head=;break;
        case 5:head=;break;
        case 6:head=;break;
        case 7:head=;break;*/
        case 8:h=create();
               print(h);break;
    }
}

book.txt内容为:

1 a1 a a2 1999.1.1 20
2 b1 b b2 2000.1.1 30
3 c1 c c2 2000.1.2 30
4 d1 d d2 2001.1.1 40
5 e1 e e2 2001.1.1 30

qq_慕后端6219475
浏览 1018回答 0
0回答

慕斯4360584

JUVLIVUBLJCQCEMGZUNGNAGYRNDJSVPTQTFLVSFEORLVKCLBZTUXHVYEQFBBRMFFCCZYFVOOHEPWDNGVDDGIGPUXXWPGKPYTKUQRQKOVWWZPNTMSRRPCPPWPWGPJJBBYUXOENBKLHFPCCCTMUBLMMCHRBBINJBVYBSPJWOKCTSSITXFIVBHRNAYCJGIPYEONUAQZJKGFVMCTNKKCRBABVRZISVSBOAPZUSSCCZPQWYPRSAMMNXAOFPFOAGDYRXSLIIGKGWDNQJVIMFZREOYBVLBSPSCZZCXMCTNUTABKHLNXRFEEPOSFSYXKDNJDAROVKJDZWSRIJJWHERFZXBBBLBQWAWNWMCTZPBRRYVBLEVDNILOYYRFCAKSKKKHEILBYIRJXNQNLLYLORXTUEBHVFPPIQQLLBUBVUXDJKODAXIVJVLXQNQXXHRJDQCVPQNGXBLEVBNXRDXDWXGCCBRBRDUOEOMBRKKKAKHKYIOBDXLYKERAYXCPDTCDIPPOXQWQAPWFPCIANOHUDQVSMZLEEYYXUUUQPRYEYIRANAEGPYSANXUKRSLRRBZGQUOZPOKADGLJZVWGPLLUYHHTYNGFANDCKJSCMCKGXDRKAGWBVKKU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