#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "introprog_structs_lists_input.h"
#define MAX_STR 255
typedef struct _element{
char title[255];
char author[255];
int year;
long long int isbn;
struct _element *next;
} element;
typedef struct _list {
element *first;
int count;
} list;
element *insert_at_begin(element *first, element *new_elem) {
new_elem->next = first;
first = new_elem;
return first;
}
element *construct_element(char *title, char* author, int year, long long int isbn) {
element *new_elem = malloc(sizeof(element));
new_elem->title = *title;//报错的是这里上下两行,incompatible types when assigning to type 'char[255]' from type 'char *'
new_elem->author = author;//
new_elem->year = year;
new_elem->isbn = isbn;
return new_elem;
}
void free_list(list *alist) {
free(alist);
}
void read_list(char* filename, list *alist) {
element* new_elem;
char* title;
char* author;
int year;
long long int isbn;
read_line_context ctx;
open_file(&ctx, filename);
while(read_line(&ctx, &title, &author, &year, &isbn) == 0) {
new_elem = construct_element(title, author, year, isbn);
alist->first = insert_at_begin(alist->first, new_elem);
alist->count++;
}
}
list* construct_list() {
list *alist = malloc(sizeof(list));
alist->first = NULL;
alist->count = 0;
return alist;
}
void print_list(list *alist) {
printf("Meine Bibliothek\n================\n\n");
int counter = 1;
element *elem = alist->first;
while (elem != NULL) {
printf("Buch %d\n", counter);
printf("\tTitel: %s\n", elem->title);
printf("\tAutor: %s\n", elem->author);
printf("\tJahr: %d\n", elem->year);
printf("\tISBN: %lld\n", elem->isbn);
elem = elem->next;
counter++;
}
}
int main(int argc, char** argv) {
list *alist = construct_list();
read_list(argc>1?argv[1]:"buecherliste.txt", alist);
print_list(alist);
free_list(alist);
return 0;
}
*title *author返回的是英文书名和英文作者名
错误的在文中//标出了,试过new_elem->title[0] = *title;这样就是一个英文书名的首字母,而不是全部;new_elem->title[255] = *title;则是返回为空了。怎么样才能使new_elem返回全部的英文书名?
代码可能有点多,我自己都反复看过了,实在是找不到怎么解决,求助各位大神。。
另外读取文件的函数是老师给的,应该是没有问题的,如果需要再贴
试过出来有问题。。。 请问具体怎么写?怎么把*title赋值给new_elem->title?
试过出来有问题。。。 请问具体怎么写?怎么把*title赋值给new_elem->title?
追答strcpy(new_elem->title, title)
追问运行正常了~非常感谢!
追答不谢,祝学习顺利
本回答被提问者和网友采纳