파일에서 일부 정보를 이중 연결 목록의 노드로 읽을 수있는 함수를 작성하려고합니다. 각 노드 데이터의 형식은 다음과 같습니다.중첩 된 구조체가있는 연결된 목록에 쓰기
구조체 (이름 기록)
아티스트
앨범
노래
장르
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고마워요!
동적 메모리를 사용할 때 명시 적으로 "malloc"해야합니다. ''tempLength''를 포인터로 선언하고 결코 메모리를 할당하지 않으면 문제가 발생합니다. –
@SimonGibbons 헤드 노드를 초기화 할 때 메모리가 이미 할당되지 않았습니까?(주에서 발생) – user3482104
예를 들어''tempLength-> mins = tempMins;''할 때 네가 메모리를 전혀 모르는''tempLength''를 사용하고있는 것은 가능하지만 이전에 해당 구조를 할당했습니다. –