将文件中的信息加载到c中的列表中

Loading info from a file into a list in c

我正在编写一个程序,它将逐行从文件中读取信息,然后将信息加载到双向链表中。信息作为名为 tvshow 的结构加载:

typedef struct tvshow{
    char name[20];
    int epNumber;
    int year;
    float duration;
    char country[3];
    char genre[20];
}tvshow;

typedef struct node{
    tvshow inf;
    struct node *next;
    struct node *prev;
}node;

void insert(node **, tvshow);
void print(node *);
FILE *safe_open(char *, char *);
void load(node **, FILE *);

int main(){
    node *head;
    head=NULL;

    FILE *in=safe_open("tvshows.txt", "r");

    load(&head, in);
    print(head);

    fclose(in);

    return 0;
    }

void insert(node **head, tvshow data){
    node *new=malloc(sizeof(node));
    new->inf=data;
    new->next=NULL;

    if (*head==NULL){
        *head=new;
        new->prev=NULL;
    }else{
        node *temp=*head;
        while (temp->next!=NULL){
        temp=temp->next;
     }
    temp->next=new;
    new->prev=temp;
    }
}

void print(node *head){
    while (head!=NULL){
        printf("%s %d %d %f %s %s\n", head->inf.name,head->inf.epNumber,head->inf.year, head->inf.duration,head->inf.country, head->inf.genre);
        head=head->next;
    }
}

FILE *safe_open(char *name, char *mode){
    FILE *fp=fopen(name, mode);

    if (fp==NULL){
        printf("failed to open");
        exit(1);
    }

    return fp;
}

void load(node **head, FILE *fp){
    tvshow temp;

    while (fscanf(fp, "%s %d %d %f %s %s", temp.name, &temp.epNumber,&temp.year, &temp.duration, temp.country, temp.genre)!=EOF){
        insert(head, temp);
    }
}

这是 .txt 文件中的示例行: TheSopranos 86 1999 55 USA Drama

困扰我的是,当我 运行 程序时,它打印以下内容:

TheSopranos 86 1999 55 USADrama Drama

为什么选择 USADrama?哪里出错了?

将 '\0' 空字符的 char country[3] 更改为 char country[4]