Karamanis
Karamanis

Reputation: 73

C:How to load a list from a file and save it at the end of the program

I'm working on two functions. The first one loads a list from a file into the program (at the start) and the second one saves the list's data to the file. I've tried to write the two functions but I'm having a hard time(I'm not really good at programming). Here are the structs I'm using I'm also adding the define of index and the creation of the list:

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

Here is the function to load the file data into the list (at the start of the program). Obviously it is wrong but I don't know how to load the data to each node since the list is empty:

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

And here is the function that save all of the list's data at the end of the program :

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

The data in the file should look kind like this:

35 Nick
29 Jim
19 Helen

Well, ofcourse the functions don't do that as they are. So I need some help to improve them. Any tips (on files) and help is much appreciated.I'm sorry for the long post. Thank you for your time.

Upvotes: 2

Views: 3427

Answers (1)

BLUEPIXY
BLUEPIXY

Reputation: 40145

To fix like this

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

Upvotes: 2

Related Questions