0
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;
    }
}
  • 1
    `fscanf()` should work. Show your code that you tried but didn't work. – Barmar May 31 '21 at 16:07
  • i tried to use fscanf() i didn't make a code for loading yet just tried to use it couldn't figure out how it works read a lot about it and still understood nothing – Hillskiller May 31 '21 at 16:08
  • `fscanf(infile, "%s %d %s", name, &duration, path);` – Barmar May 31 '21 at 16:10
  • i don't get it like what does it do exactly? enters %s into char array called name and %d into an int called duration....? – Hillskiller May 31 '21 at 16:11
  • 1
    It reads from the file, and puts each of those fields into the corresponding variables. – Barmar May 31 '21 at 16:11
  • 1
    You do this in a loop, and then copy those values into linked list nodes. – Barmar May 31 '21 at 16:12
  • i tried to do this but it's printing out nothing https://pastebin.com/TfrcCR6X – Hillskiller May 31 '21 at 16:14
  • `== 1` should be `== 3` since you're reading 3 variables, not 1 variable. – Barmar May 31 '21 at 16:15
  • this works now could you please explain why changing it to 3 works? – Hillskiller May 31 '21 at 16:16
  • It returns the number of values that were extracted from the file. You're extracting 3 values: name, duration, and path – Barmar May 31 '21 at 16:18
  • i see im going to try to make the function now i'll keep you updated – Hillskiller May 31 '21 at 16:19
  • i tried to do this https://pastebin.com/JmXEUa01 it doesn't link the list but it does make the frames – Hillskiller May 31 '21 at 16:23
  • Is the name field always a number like in your example? If so, please rename `name` to `number` and use an integer type. If not and unless the name cannot contain a space, then your file format is broken: as I said in a comment of your previous question, use double quotes. – fpiette May 31 '21 at 16:39
  • @ryyker There's no harm in using the same name for the struct and typedef. The struct name is redundant, but harmless. – Barmar May 31 '21 at 17:03
  • See https://stackoverflow.com/questions/13431108/changing-address-contained-by-pointer-using-function regarding the `list` parameter to `load` – Barmar May 31 '21 at 17:05
  • @Barmar - Ummm you are completely right. Other than being a little confusing (to me at least.) it is syntactically acceptable. And because its just a personal bias, I have removed the comment. Thanks. – ryyker May 31 '21 at 17:36

0 Answers0