简体   繁体   English

用嵌套结构写入链表

[英]Writing to a linked lists with nested structs

I am trying to write a function that can read some info from a file into a node in a doubly linked list. 我正在尝试编写一个函数,该函数可以将文件中的某些信息读取到双向链表中的节点中。 The format for the each nodes data is as follows. 每个节点数据的格式如下。

struct(named record) struct(命名记录)
artist 艺术家
album 专辑
song 歌曲
genre 类型
songLength(This is another struct that contains mins and secs) songLength(这是另一个包含分钟和秒的结构)
playcount 播放计数
rating 评分

void load(FILE *file, Node *head)
{
    char tempArtist='\0', tempAlbum='\0', tempTitle='\0', tempGenre='\0'
    ,tempSpace='\0',tempMins='\0',tempSecs='\0';
    SongLength *tempLength=NULL;
    int tempPlay=0, tempRating=0,test=0;
tempLength = (SongLength*)malloc(sizeof(SongLength));

    fscanf(file,"%s",&tempArtist);
    fscanf(file,"%s",&tempAlbum);
    fscanf(file,"%s",&tempTitle);
    fscanf(file,"%s",&tempGenre);
    fscanf(file,"%s",&tempMins);
    fscanf(file,"%s",&tempSecs);
    fscanf(file,"%s",&tempPlay);
    fscanf(file,"%s",&tempRating);
    fscanf(file,"%s",&tempSpace);

    tempLength->mins=tempMins; 
    tempLength->secs=tempSecs;

    head->data->album=tempAlbum; // breaks here
    head->data->artist=tempArtist;
    head->data->genre=tempGenre;
    head->data->song=tempTitle;
    head->data->length=tempLength;
    head->data->played=tempPlay;
    head->data->rating=tempRating;

}

This is my current load function. 这是我当前的加载功能。 When attempting to store these values in to nodes data I get an access violation. 当尝试将这些值存储到节点数据中时,出现访问冲突。

Here are my structs for easy reproduction 这是我的结构,易于复制

typedef struct songlength
{
    int mins;
    int secs;
}SongLength;


typedef struct record
{
    char artist;
    char album;
    char song;
    char genre;
    struct songlength *length;
    int played;
    int rating;

}Record;

typedef struct node
{
    struct node *pPrev;
    struct node *pNext;
    struct record *data;

}Node;

makeNode makeNode

Node *makeNode(Record *newData)
{
    Node *temp = NULL;

    temp=(Node*)malloc(sizeof(Node));

    temp->data=newData;
    temp->pNext=NULL;

    return temp;
}

If any confusion arises just let me know! 如果有任何混淆,请告诉我! Also this is my first experience with dynamic memory so be gentle :P 这也是我对动态内存的第一次体验,所以要小心:P

Thanks! 谢谢!

These lines are not right. 这些行是不正确的。

fscanf(file,"%s",&tempArtist);
fscanf(file,"%s",&tempAlbum);
fscanf(file,"%s",&tempTitle);
fscanf(file,"%s",&tempGenre);
fscanf(file,"%s",&tempMins);
fscanf(file,"%s",&tempSecs);
fscanf(file,"%s",&tempPlay);
fscanf(file,"%s",&tempRating);
fscanf(file,"%s",&tempSpace);

They will definitely lead to undefined behavior because of the way the variables are defined. 由于定义变量的方式,它们肯定会导致未定义的行为。

You cannot expect 你不能期待

char c = '\0';
fscanf(file, "%s", &c);

to work. 上班。 There isn't enough memory at &c to read a string. &c没有足够的内存来读取字符串。 You need something like: 您需要类似:

char s[100]; // Or some size that is large enough to hold the data
             // you are about to read.
fscanf(file, "%99s", s); // Make sure that you don't read more than 99
                         // characters. Leave at least one character
                         // for the terminating null character.

I hope that gives you enough clues on how to change your variables. 我希望这为您提供了有关如何更改变量的足够线索。

You did not assign memory for the variable tempLength to point to. 您没有为变量tempLength指向分配内存。

Add this before accessing the elements 在访问元素之前添加它

SongLength *tempLength = malloc(sizeof(struct(SongLength));

EDIT 编辑

I'm just giving an overall idea how to allocate and use nested structs for your case 我只是给出一个总体思路,如何为您的案例分配和使用嵌套结构

Node *head;
Record *r=malloc(sizeof(struct record));
SongLength *s=malloc(sizeof(struct songlength));
r->length=s;//<----- 1
r->length->mins=10;//Now you can assign values

head=malloc(sizeof(struct node));
head->pPrev=NULL;
head->pNext=NULL;
head->data=r;//<--- The length member inside record is already assigned memory in 1

head->data->artist='c';
head->data->length->mins=10;//assign

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM