void save(FrameNode* list)
{
FILE* fileSaver = NULL;
fileSaver = fopen("C:\\Users\\magshimim\\Desktop\\test\\savedData.txt", "w+");
FrameNode* curr = list;
while (curr)
{
fprintf(fileSaver, "%s %d %s\n", curr->frame->name, curr->frame->duration, curr->frame->path);
curr = curr->next;
}
fclose(fileSaver);
printf("Saved successfully!\n");
}
output into file:
1 500 C:\Users\magshimim\Desktop\test\tt.png
2 6969 C:\Users\magshimim\Desktop\test\tt.png
i have a function that saves the linked list now it saves it as in the output into text file splitted by spaces and new line
order in saving: name, duration, path
how could i load that back into a linked list the structure:
// Frame struct
typedef struct Frame
{
char* name;
unsigned int duration;
char* path;
} Frame;
// Link (node) struct
typedef struct FrameNode
{
Frame* frame;
struct FrameNode* next;
} FrameNode;
and load them back in order is important too
i read about fscanf() i tried using it to parse data still couldn't do it.
each frameNode has a frame which contains in it the data, and there's next in frameNode which is used to make the linked list
i have a function to create a frameNode:
/**
Function will create a frame
input:
image name, duration and path
output:
the image with the updated info
*/
FrameNode* createFrame(FrameNode* head, char name[], char path[], unsigned int duration)
{
int nameExist = doesNameExist(head, name); //doesNameExist() checks if a name already exists in the linked list returns 1 if a name exist, 0 if not
if (nameExist)
{
printf("Name already exists!\n");
return 0;
}
FrameNode* newFrame = (FrameNode*)malloc(sizeof(FrameNode));
newFrame->frame = (Frame*)malloc(sizeof(Frame));
newFrame->frame->name = (char*)malloc(STR_LEN);
newFrame->frame->path = (char*)malloc(STR_LEN);
newFrame->frame->duration = (unsigned int*)malloc(sizeof(unsigned int));
strncpy(newFrame->frame->name, name, STR_LEN);
strncpy(newFrame->frame->path, path, STR_LEN);
newFrame->frame->duration = duration;
newFrame->next = NULL;
return newFrame;
}
function to add the linked list to the end:
/**
Function will add an image to the frames
input:
newNode - the new person to add to the list
output:
none
*/
void insertAtEnd(FrameNode* newNode, FrameNode** head)
{
if (!(*head))
{
*head = newNode;
}
else
{
FrameNode* p = (*head);
while (p->next)
{
p = p->next;
}
p->next = newNode;
}
}