Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c语言中从文件到链表的数据解析_C_List_Linked List_Load_Gif - Fatal编程技术网

c语言中从文件到链表的数据解析

c语言中从文件到链表的数据解析,c,list,linked-list,load,gif,C,List,Linked List,Load,Gif,输出到文件: void save(FrameNode* list) { FILE* fileSaver = NULL; fileSaver = fopen("C:\\Users\\magshimim\\Desktop\\test\\savedData.txt", "w+"); FrameNode* curr = list; while (curr) { fprintf(fileSaver, &q

输出到文件:

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");
}
我有一个保存链表的函数,现在它将链表保存为输出中的文本文件,并按空格和新行分隔

保存顺序:名称、持续时间、路径

如何将其加载回结构的链表中:

1 500 C:\Users\magshimim\Desktop\test\tt.png
2 6969 C:\Users\magshimim\Desktop\test\tt.png
按顺序加载它们也很重要

我读过关于fscanf()的文章,我试着用它来解析数据,但还是做不到

每个frameNode都有一个包含数据的框架,frameNode中还有一个用于创建链接列表的下一个框架

我有一个创建框架节点的函数:

// 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;
用于将链表添加到末尾的函数:

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

fscanf()
应该可以工作。显示您尝试过但不起作用的代码。我尝试过使用fscanf(),我没有编写用于加载的代码,只是尝试过使用它无法理解它是如何工作的阅读了很多关于它的内容,但仍然不理解fscanf(填充,“%s%d%s”、名称和持续时间、路径)它从文件中读取,并将每个字段放入相应的变量中。您可以在循环中执行此操作,然后将这些值复制到链表节点中。
/**
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;
    }
}