【问题标题】:Linked Files instead of linked list链接文件而不是链接列表
【发布时间】:2016-05-15 16:15:37
【问题描述】:

我有一个包含时间开始和时间结束元组的链接列表。在每个列表中,这些元组不重叠。 现在,我不想将这些元组放在一个列表中,而是希望它们存储在一个文件中。 因此,列表现在应该是一个文件。为了对这些文件进行排序并遍历每个文件,我必须知道文件旁边是哪个文件。换句话说,我怎样才能使指针指向下一个文件,例如在链表中。

    typedef struct List
{
  struct List *next;


} List;

但我不想要这样的东西,我先创建链表,然后将每个列表放在一个文件中,因为我想逃避内存使用。喜欢这里

static void create_files(List* first){

List* tmp = first;
int partition_num = 1;

while(tmp != NULL){
    char filename[29];
    sprintf(filename, "Partitions%d/Partition%d.txt",partition_folder, partition_num);
    File* partition;
    partition= fopen(filename, "w");


    while(tmp->head != NIL){

        fprintf(partition,"[%d, %d) \n",ts,te);
        tmp->head= tmp->head->next;

    }
    list_num++;
    tmp=tmp->next;

}

更多类似的东西

File* firstfile;
{ //adding data to this file}
File* second_file;
{ // adding data to this file}
firstfile ->next = second file;

所以我想要一个链接文件之类的东西。有什么建议吗?

【问题讨论】:

  • 您是在问如何将一个文件的信息放入另一个文件中?把它打印在那里..
  • 抱歉,我认为您应该得到一些帮助来改写您的问题。就目前而言,很难理解你想要什么。

标签: c list file memory linked-list


【解决方案1】:

这样定义

struct FileLinkedList
{
    FILE * f;
    FileNode * next;
}

但你需要在文件中有一些元数据,例如文件中的第一行是下一个文件的文件名或 0 结束

A.txt

B.txt
This is the first Node in the FileLinkedList

B.txt

0
This is the last Node in the FileLinkedList

列表

{ [A.txt] }--> { [B.txt] }--> 0

然后定义函数为你生成列表

FileLinkedList* createFileLinkedList (char* file_name)
{
    char next_file[255];
    FileLinkedList* n,head = (FileLinkedList*)malloc(sizeof(FileLinkedList));
    head->f = fopen(file_name);
    next_file = get_meta(head->f);
    init_reader(head->f);

    n = head;
    while (strcmp("0",next_file) != 0)
    {
        n->next = (FileLinkedList*)malloc(sizeof(FileLinkedList));
        n = n->next;
        n->f = fopen(next_file);
        next_file = get_meta(head->f);
        init_reader(head->f);
    }

    return head;
}

get_meta 在哪里读取我们需要的第一行

char* get_meta (FILE* f)
{
    char* m = (char*)malloc(sizeof(char)*255);
    rewind(f);
    fgets(m, sizeof(m), f);
    return m;
}

init_reader是将文件光标放在元数据部分之后

void init_reader (FILE* f)
{
    rewind(f);
    while(fgetc(f) != '\n');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-01
    • 2012-09-27
    • 1970-01-01
    • 2016-05-22
    • 2013-10-19
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    相关资源
    最近更新 更多