简体   繁体   中英

Linked list in file read\write

I have a project in c to do a movie maker and i put frames and i need to do option to save the project and open it again and enter again frames how i do that?

this is the structs

typedef struct Frame
{
char*       name;
unsigned int    duration;
char*       path;  
} Frame;


// Link (node) struct
typedef struct FrameNode
{
    Frame* frame;
    struct FrameNode* next;
} FrameNode;

this is the option i need to do i did all except the save and open option

open path Add new frame Remove a frame Change frame index Change frame duration Change duration of all frames List frames Play movie! Save project

Saving will require you to write to a file. You can step through your linked list and at each node write to a file something of the form (name,duration,path) and then to open you would just read from the file. See this tutorial page for info on how to read and write from a file.

You can create a save and a load function:

void save(Frame frame){
    FILE *file = fopen("/path/to/save.txt", "w");
    if(file != NULL){
        fwrite(&frame, sizeof(Frame), 1, file);
        fclose(file);   
    }
    else
        printf("Error %d\n", errno);
}

Frame load(void){
    Frame frame;
    FILE *file = fopen("path/to/save.txt", "r");
    if(file != NULL){
        fread(&frame, sizeof(Frame), 1, file);
        fclose(file);
    }
    else
        printf("Error %d\n", errno);
    return frame;
}

And then in your main function:

int main(void){ 
    Frame f = {
        .name = "my_frame", 
        .duration = 60, 
        .path = "/path/to/frame"
    };
    save(f);
    Frame g = load(); // It will load f to g
    printf("%s %d %s\n", g.name, g.duration, g.path);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM