简体   繁体   English

C链表 - 写指向文件的指针

[英]C linked list - Write pointer to file

I have the following struct: 我有以下结构:

typedef struct transgressor
{
    int priority;
    Record record;
    struct transgressor *next;
    int position;
    int hasNext;
}Transgressor;

I need to write it to file. 我需要把它写到文件中。 Problem is with struct transgressor *next part because it's pointer. 问题在于struct transgressor *next part,因为它是指针。 How can I dereference it, or organize struct different, so I can write it and after read from file? 我如何取消引用它,或组织结构不同,所以我可以写它并从文件中读取后?

There's no point in saving the pointer to file. 将指针保存到文件没有意义。 Since you already have a position variable in your struct, you can use that to help you re-create the list in memory instead when loading the list from file. 由于结构中已经有一个position变量,因此在从文件加载列表时,可以使用它来帮助您在内存中重新创建列表。

Before you save anything to a file, you should specify (ideally as a written document) the format in which the data will be stored in the file at the byte level. 在将任何内容保存到文件之前,应指定(理想情况下为书面文档)数据在字节级别存储在文件中的格式。 Use an existing file format if there is one that's convenient, otherwise you have to design and specify your own. 如果有方便的话,请使用现有的文件格式,否则您必须设计并指定自己的文件格式。

Once you've decided on the format for the data at the byte level, write code to convert the structure to and from that format. 一旦确定了字节级数据的格式,就编写代码将结构转换为该格式或从该格式转换。 What that format to the disk. 磁盘的格式是什么。 Read that format from the disk. 从磁盘读取该格式。

There are no shortcuts that don't lead to pain and suffering. 没有捷径不会导致痛苦和痛苦。

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

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