簡體   English   中英

用嵌套結構寫入鏈表

[英]Writing to a linked lists with nested structs

我正在嘗試編寫一個函數,該函數可以將文件中的某些信息讀取到雙向鏈表中的節點中。 每個節點數據的格式如下。

struct(命名記錄)
藝術家
專輯
歌曲
類型
songLength(這是另一個包含分鍾和秒的結構)
播放計數
評分

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;

}

這是我當前的加載功能。 當嘗試將這些值存儲到節點數據中時,出現訪問沖突。

這是我的結構,易於復制

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

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

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

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

    return temp;
}

如果有任何混淆,請告訴我! 這也是我對動態內存的第一次體驗,所以要小心:P

謝謝!

這些行是不正確的。

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);

由於定義變量的方式,它們肯定會導致未定義的行為。

你不能期待

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

上班。 &c沒有足夠的內存來讀取字符串。 您需要類似:

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.

我希望這為您提供了有關如何更改變量的足夠線索。

您沒有為變量tempLength指向分配內存。

在訪問元素之前添加它

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

編輯

我只是給出一個總體思路,如何為您的案例分配和使用嵌套結構

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