Thread: Saving linked list in file

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    1

    Saving linked list in file

    Hello.
    I'm programming a program to make a mp4 player.
    After finishing the program I need to add and option to save the linked list for using it later. I've succeeded to save all the content of the list to a txt file, but fail to get it back to the list.

    linkedList.h:

    Code:
    #ifndef LINKEDLISTH
    #define LINKEDLISTH
    
    
    
    
    #include "Frame.h"
    struct Link
    {
        frame_t    *frame;
        struct Link    *next;
        struct Link *prevoius;
    };
    
    
    
    
    
    
    
    
    typedef struct Link    link_t;
    #endif
    Frame.h

    Code:
    ifndef FRAME_H
    #define FRAME_H
    
    
    #include <stdio.h>
    
    
    struct Frame
    {
        char            *name;
        unsigned int    duration;
        char            *path;  // may change to FILE*
    };
    
    
    typedef struct Frame frame_t;
    
    
    
    
    #define MAX_PATH_SIZE (256)
    #define MAX_NAME_SIZE (50)
    
    
    #endif //FRAME_H
    linkedList.c

    Code:
    int writeListTo,File(const link_t *list);
    int readListFromFile(const link_t *list);
    
    
    int readListFromFile(const link_t *list){
        FILE *pfile = fopen("s.txt", "r");
        fseek(pfile, SEEK_SET, 0);
        if (pfile != NULL) {
            while (!feof(pfile)) {
                fscanf(pfile, "%s %s %d", &list->frame->name, &list->frame->path, &list->frame->duration);
                            
            }
    
    
            fclose(pfile);
            return 1;
    
    
        }
        else{
            printf("Error opening file");
            fclose(pfile);
    
    
        }
    }
    
    
    int writeListToFile(const link_t *list) {
        int c = 0;
        FILE *pfile = fopen("SavedList.txt", "w");
        if (pfile != NULL) {
            for (; list != NULL; list = list->next) {
                fprintf(pfile, "%s %s %d ", list->frame->name, list->frame->path, list->frame->duration);
    
    
    
    
            }
        }
        fclose(pfile);
        return 1;
    }
    What am I doing wrong?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You're not allocating space for any of these things
    - struct Link
    - struct Frame
    - struct Frame.name
    - struct Frame.path


    Code:
    struct Frame *temp = malloc( sizeof(*temp) );
    if ( temp ) {
        temp->name = malloc(MAX_NAME_SIZE);
        temp->path = malloc(MAX_PATH_SIZE);
        fscanf(pfile, "%s %s %d", temp->name, temp->path, &temp->duration);
        // now append temp to your list, by allocating a link and fixing up the frame/prev/next pointers.
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by Yoni Goikhman View Post
    Code:
    int writeListTo,File(const link_t *list);
    int readListFromFile(const link_t *list);
    
    
    int readListFromFile(const link_t *list){
        FILE *pfile = fopen("s.txt", "r");
        fseek(pfile, SEEK_SET, 0);
        if (pfile != NULL) {
            while (!feof(pfile)) {
                fscanf(pfile, "%s %s %d", &list->frame->name, &list->frame->path, &list->frame->duration);
                            
            }
            fclose(pfile);
            return 1;
        }
        else{
            printf("Error opening file");
            fclose(pfile);
        }
    }
    Don't work with a filepointer before you check it.
    This means, do 'fseek' after check 'if (pfile != NULL)'.
    Second, you don't need 'fseek', because if you open a file for reading, the position for read will be automaticly set at the beginning of the file.
    Your function returns an 'int'.
    If the file was opened without problems, you return '1' after closing the file.
    But if the file was not opened, you output a error message (on stdout), close a file that was never opened and then? What returns the function?
    All in all, you read the information allways in the same structure. At the end you have only one 'frame_t' with valid values.


    Quote Originally Posted by Yoni Goikhman View Post
    Code:
    int writeListToFile(const link_t *list) {
        int c = 0;
        FILE *pfile = fopen("SavedList.txt", "w");
        if (pfile != NULL) {
            for (; list != NULL; list = list->next) {
                fprintf(pfile, "%s %s %d ", list->frame->name, list->frame->path, list->frame->duration);
            }
        }
        fclose(pfile);
        return 1;
    }
    You write all information in one line. There is no linefeed in your 'printf'.
    I think this will be difficult to read in later (especially, your function 'readListFromFile' will not handle this format).
    If the file was not opened correctly, you close a NULL pointer.
    The function returns allways 1. Normally, the returned value reflect the success or fail of the function.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Saving in a File the Information of a Nested List
    By José Aguilera in forum C Programming
    Replies: 3
    Last Post: 03-26-2016, 06:37 PM
  2. Saving Linked List
    By Blacky Ducky in forum C++ Programming
    Replies: 11
    Last Post: 06-15-2011, 01:56 AM
  3. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  4. Saving linked lists to a binary file
    By kotoko in forum C Programming
    Replies: 1
    Last Post: 06-14-2008, 06:41 PM
  5. Saving and Loading Linked Lists With File i/o
    By Red Army in forum C++ Programming
    Replies: 8
    Last Post: 05-15-2002, 03:19 PM

Tags for this Thread