简体   繁体   English

文件读\\写中的链表

[英]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?我在 c 中有一个项目来制作电影制作人,我放了帧,我需要选择保存项目并再次打开它并再次输入帧,我该怎么做?

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.您可以单步执行您的链表,并在每个节点向文件写入某种形式(name,duration,path) ,然后打开您只需从文件中读取即可。 See this tutorial page for info on how to read and write from a file.有关如何读取和写入文件的信息,请参阅本教程页面

You can create a save and a load function:您可以创建saveload功能:

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:然后在您的main功能中:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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