马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如题,我写的这个程序在文件中的keyword如果出现了空格或者直接为空(只有一个回车的时候)程序不能正常运行,不知道怎么解决#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct book //书籍信息的结构
{
int book_id;
char book_name[30]; //一个书的id、一个书名、5个关键字、3个作者、一个发布地、一个发布时间
char keyword1[30];
char keyword2[30];
char keyword3[30];
char keyword4[30];
char keyword5[30];
char author1[30];
char author2[30];
char author3[30];
char publisher[30];
char publish_time[30];
struct book *next;
}*Book;
struct book *data_book_input(); //将数据库数据导出到一个结构指针上
void main()
{
struct book *head;
head = data_book_input(); //head是头指针
printf("%d\n",head->book_id); //这里是测试用
printf("%s\n",head->book_name);
head = head->next;
printf("%d\n",head->book_id);
printf("%s\n",head->book_name);
}
struct book *data_book_input() //将数据库数据导出到一个结构指针上
{
struct book *p,*head,*tail;
head = tail = NULL;
int book_id;
char book_name[30];
char keyword1[30];
char keyword2[30];
char keyword3[30];
char keyword4[30];
char keyword5[30];
char author1[30];
char author2[30];
char author3[30];
char publisher[30];
char publish_time[30];
FILE *f = NULL;
if((f=fopen("data_book.txt","r")) == NULL)
{
printf("File is Error! No1\n");
return NULL;
}
while(fscanf(f, "%d %s %s %s %s %s %s %s %s %s %s %s",
&book_id,book_name,keyword1,keyword2,keyword3,keyword4,keyword5,
author1,author2,author3,publisher,publish_time) != EOF)
{
if((p = (struct book *)malloc(sizeof(struct book)))==NULL) //分配储存空间
{
printf("Error!");
return NULL;
}
p->book_id = book_id; //导入数据
strcpy(p->book_name , book_name);
strcpy(p->keyword1 , keyword1);
strcpy(p->keyword2 , keyword2);
strcpy(p->keyword3 , keyword3);
strcpy(p->keyword4 , keyword4);
strcpy(p->keyword5 , keyword5);
strcpy(p->author1 , author1);
strcpy(p->author2 , author2);
strcpy(p->author3 , author3);
strcpy(p->publisher , publisher);
strcpy(p->publish_time , publish_time);
p->next = NULL; //顺序建立链表
if(head == NULL)
head = p;
else
tail->next = p;
tail = p;
} //链表头地址是head,最后的next是NULL
fclose(f);
return head; //返回头指针
}
|