2015-02-07 2 views
0

파일에서 일부 정보를 이중 연결 목록의 노드로 읽을 수있는 함수를 작성하려고합니다. 각 노드 데이터의 형식은 다음과 같습니다.중첩 된 구조체가있는 연결된 목록에 쓰기

구조체 (이름 기록)
아티스트
앨범
노래
장르
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

고마워요!

+0

동적 메모리를 사용할 때 명시 적으로 "malloc"해야합니다. ''tempLength''를 포인터로 선언하고 결코 메모리를 할당하지 않으면 문제가 발생합니다. –

+0

@SimonGibbons 헤드 노드를 초기화 할 때 메모리가 이미 할당되지 않았습니까?(주에서 발생) – user3482104

+0

예를 들어''tempLength-> mins = tempMins;''할 때 네가 메모리를 전혀 모르는''tempLength''를 사용하고있는 것은 가능하지만 이전에 해당 구조를 할당했습니다. –

답변

1

변수 tempLength에 대한 메모리를 지정하지 않았습니다.

는 요소에게 내가 얼마나 할당하고 귀하의 경우

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 
+0

이 작업은 스캔 된 데이터를 노드에 할당하려고 할 때 중단됩니다. 내가 만든 임시 변수 중 하나 하나를 malloc해야할까요? – user3482104

+0

@ user348104'makeNode'에 전달하기 전에'record' 내부에'songlength'에 대한 메모리를 할당해야합니다. – user7

+0

'char'는'하나의 문자'만 저장할 수 있습니다. sahu의 답변 상태로 배열을 사용해야합니다. – user7

2

이를 위해 중첩 된 구조체를 사용하는 전체 아이디어를주는거야

SongLength *tempLength = malloc(sizeof(struct(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); 

변수가 정의 된 방식으로 인해 정의되지 않은 동작이 발생합니다.

당신은

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. 

변수를 변경하는 방법에 대한 충분한 단서가 있기를 바랍니다.