I am trying to load an unknown amount of data from a file in to a linked list
Load Function
void load(FILE *file, Node **head) // while feof somewhere
{
char tempArtist[30]={'\0'}, tempAlbum[30]={'\0'}, tempTitle[30]={'\0'}, tempGenre[30]={'\0'},tempSpace='\0';
char tempPlay[100]={'\0'}, tempRating[6]={'\0'}, tempMins[8]={'\0'}, tempSecs[8]={'\0'};
Data *temp;
temp=(Data*)malloc(sizeof(Data));
while(!feof(file))
{
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);
temp->mins=strdup(tempMins);
temp->secs=strdup(tempSecs);
temp->album=strdup(tempAlbum);
temp->artist=strdup(tempArtist);
temp->genre=strdup(tempGenre);
temp->song=strdup(tempTitle);
temp->played=strdup(tempPlay);
temp->rating=strdup(tempRating);
insertFront(head,temp);
}
}
The problem I am having is that when I go to print out the list all the entries are the same as the last one read from the file. This is something to do with the strdup() but I can't get it to copy to the data type without getting access violations.
What is another method(proper method) that I could use to copy the strings read from the file and pass them to the insert?
InsertFront
void insertFront(Node **head, Data *data)
{
Node *temp=makeNode(data);
if ((*head) == NULL)
{
*head=temp;
}
else
{
temp->pNext=*head;
*head=temp;
}
}
Data struct
typedef struct data
{
char *artist;
char *album;
char *song;
char *genre;
char *played;
char *rating;
char *mins;
char *secs;
}Data;
testing file
snoop
heartbeat
swiggity
rap
03
10
25
4
hoodie
cakeboy
birthday
hiphop
02
53
12
5