C:如何从文件加载列表并将其保存在程序的末尾

时间:2016-05-19 04:54:55

标签: c list struct save

我正在研究两个功能。第一个将列表从文件加载到程序中(在开始时),第二个将列表的数据保存到文件中。我试着写这两个函数但是我很难(我不擅长编程)。以下是我正在使用的结构我还添加了索引的定义和列表的创建:

#define index 30

typedef struct dataR* data;
    struct dataR{
        int age;
        char name[index];

    };

    typedef struct nodeR* node;
    struct nodeR{
        data a;
        node next;

    };

    typedef struct listR* list;
    struct listR{
        node head, tail, curr; 
        int size;
    };

list list_create(){
    list List=malloc(sizeof(struct listR));
    assert(List);
    List->head=NULL;
    List->tail=NULL;
    List->curr=NULL;
    List->size=0;
    return List;
}

这是将文件数据加载到列表中(在程序开头)的功能。显然这是错误的,但由于列表为空,我不知道如何将数据加载到每个节点:

    list load(char *filename, list List)
    {
       FILE *fd=fopen("filename","r");
       fscanf(fd, "%d",&(List->head->a->age));
       fscanf(fd, "%s",&(List->head->a->name[index-1]));
       fclose(fd);
       fd=NULL;
    }

这是在程序结束时保存所有列表数据的函数:

    void save(char *filename, list List)
    {
       FILE *fd=fopen("filename.dat","w");
       if (fd==NULL)
       {
          printf("File does not exist");
          return;
       }
       fwrite(List->head->a, sizeof(struct dataR),1,fd);
       node tmp=List->head->next;
       while(tmp->next!=NULL)
       {
          fwrite(tmp->next->a, sizeof(struct dataR),1,fd);
          tmp=tmp->next;
       }

       fclose(fd);
       fd=NULL;
    }

文件中的数据看起来应该是这样的:

35 Nick
29 Jim
19 Helen

嗯,当然,这些功能并不像他们那样做。所以我需要一些帮助来改进它们。任何提示(关于文件)和帮助非常感谢。我很抱歉这篇长篇文章。感谢您的时间。

1 个答案:

答案 0 :(得分:2)

要像这样修复

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define S_(v) #v
#define S(v) S_(v) //stringify

#define MAX_NAME_LENGTH 30 //index is bad name

typedef struct dataR* data;
struct dataR{
    int age;
    char name[MAX_NAME_LENGTH + 1];
};

typedef struct nodeR* node;
struct nodeR{
    data a;
    node next;
};

typedef struct listR* list;
struct listR{
    node head, tail, curr;//curr unused?
    int size;
};

list list_create(void){
    list List = malloc(sizeof(*List));
    assert(List);
    List->curr = List->tail = List->head = NULL;
    List->size=0;
    return List;
}

void addList(list List, data new_data){
    node new_node = malloc(sizeof(*new_node));
    new_node->a = new_data;
    new_node->next = NULL;
    if(List->head == NULL)
        List->head = List->tail = new_node;
    else
        List->tail = List->tail->next = new_node;
    ++List->size;
}

list load(const char *filename, list List){
    FILE *fd = fopen(filename,"r");//"filename"--> filename, LOL
    if(!fd){
        fprintf(stderr, "%s can't open in load.\n", filename);
        perror("fopen");
        return NULL;
    }
    int age;
    while(EOF != fscanf(fd, "%d", &age)){// or 1 == fscanf(fd, "%d", &age)){
        data new_data = malloc(sizeof(*new_data));
        new_data->age = age;
        fscanf(fd, "%" S(MAX_NAME_LENGTH) "s", new_data->name);
        addList(List, new_data);
    }
    fclose(fd);
    //fd=NULL;//meaningless
    return List;//need return value;
}

void save(const char *filename, list List){
    FILE *fd=fopen(filename, "w");
    if(!fd){
        fprintf(stderr, "%s can't open in save.\n", filename);
        perror("fopen");
        return ;
    }
    //fwrite(List->head->a, sizeof(struct dataR),1,fd);//fwrite doesn't match load
    node tmp = List->head;
    while(tmp){
        fprintf(fd, "%d %s\n", tmp->a->age, tmp->a->name);
        tmp = tmp->next;
    }
    fclose(fd);
}

int main(void){
    list List = list_create();

    load("list.txt", List);
    printf("List hold %d data.\n", List->size);
    data addData = malloc(sizeof(*addData));
    addData->age = 73;
    strcpy(addData->name, "Ken");
    addList(List, addData);
    save("list.txt", List);
    //deallocate
    return 0;
}