How to properly build linked list with char structures

Viewed 92

I have a dynamic structure linked list and i need to create functions that:
1. will load data from text file.
2. add new element to my linked list structure.
3. output all data existing in my linked list.
I also have menu, so i call these functions to input what i need in console. I guess that i have problems with dynamic data allocation and pointers. https://imgur.com/a/8ynqar8

I've struggling with this already couple days trying solve problem with a proper allocate pointers.

    #define MES_LEN 100    
    #define m 23    
    typedef struct {    
       char index[m];    
       char name[MES_LEN];    
       char release[MES_LEN];    
       char length[MES_LEN];    
       char artist[MES_LEN];    
       } INFORM;    
    typedef struct list_elem {    
       INFORM inform;    
       struct list_elem *next;    
    }    
    LEL;    
    LEL* list;    
    void AddElement(void);    // Writing new element to file (DATAinput)
    void LoadData(void); // Reading previously written data    
    void ReadElement (void); // Reading data from existing file data (DATA)    
    void OutpuAll(void); // Output in console all previously added data from both files    
    int menu(void);    
    int main() {    
    ...                 
            case 1: OutpuAll(); break;                       // Output all data    
            case 2: AddElement(); ReadElement(); break;    // Add element to file                 
            case 3: LoadData(); break;             // Loading data from the file    
    ...
    }    
        void AddElement (void) // I have an empty file that i fill and then read and link to my main structure(this works fine)    
        {           
        char albN[25], albR[11], albL[11], albA[25], albI[5];    
        FILE * fileD;    
        fileD = fopen("DATAinput.txt", "r");    
    ...      
        printf("Enter album artist \n");    
            scanf("%s", albA);    
            fprintf(fileD, "%s ", albA);    
            fclose(fileD);    

    } 

    void ReadElement (void) // Reading previously written data(AddElement)    
    {    
        LEL *ptr;    
        ptr = (LEL*)malloc(sizeof(LEL));    
        FILE * fileD;    
        fileD = fopen("DATAinput.txt", "r");    
        if (fileD == NULL) {    
            printf("Can not open the file\n");    
            exit(1);    
        }    
        while(fscanf (fileD, "%s %s %s %s %s", ptr->inform.index, ptr->inform.name, ptr->inform.release, ptr->inform.length, ptr->inform.artist) != EOF)    
        { ptr->next=NULL;    
            printf("\n Id:%s Album: %s  Released: %s  Length: %s  Artist: %s", ptr->inform.index, ptr->inform.name, ptr->inform.release, ptr->inform.length, ptr->inform.artist); }    
        fclose(fileD);    
        free(ptr);    
    }    
    void LoadData(void)

 // Reading existing data from file    

    {    
        LEL *current, *head;    
        LEL *ptr;    
        ptr = (LEL*)malloc(sizeof(LEL));     
        FILE * file;     
        file = fopen("DATA.txt", "r");    
        if (file == NULL) {    
            printf("Can not open the file\n");    
        }    
        while(fscanf (file, "%s %s %s %s %s", ptr->inform.index, ptr->inform.name, ptr->inform.release, ptr->inform.length, ptr->inform.artist) != EOF)    
        {  printf("\n %s Album: %s  Released: %s  Length: %s  Artist: %s", ptr->inform.index, ptr->inform.name, ptr->inform.release, ptr->inform.length, ptr->inform.artist); }       
               fclose(file);    
               ptr->next = NULL;    
        }    
    void OutpuAll(void) // Trying to output data that i previously loaded from files to structure.    
    {    
        LEL *ptr; 
LEL* ptr = list;
    while (ptr!=NULL) {  
            printf("\n Id:%s Album: %s  Released: %s  Length: %s  Artist: %s", ptr->inform.index, ptr->inform.name, ptr->inform.release, ptr->inform.length, ptr->inform.artist);}     
       }
    ptr = ptr->next;
}

I have some troubles with pointers at the end of these functions

void ReadElement (void)    
void LoadData(void)    
void OutpuAll(void)

I will be grateful for help

1 Answers

There are multiple issues with the code.

  1. You are writing to the file with AddElement and then reading with ReadElement. From the second item onwards, you will be reading all the elements in the read function. So if you have written 10 albums, you will read all them in ReadElement every time. For the next write, you will discard the 10 albums read into the linked list and read 11 albums again.

ReadElement in this case is not doing functionality and can be discarded.

  1. in AddElement, you are writing only albA while you are reading many other parameters in ReadElement

  2. AddElement opens the file in "r" mode which will set the file pointer to the start of the file. You need to open it in "a" mode

There are additional issues, you can see the code below for reference.

LEL *head = NULL;
LEL *currrent = NULL;

void AddElement (void) // I have an empty file that i fill and then read and link to my main structure(this works fine)    
{           
    char albN[25], albR[11], albL[11], albA[25], albI[5];    
    FILE * fileD;    
    fileD = fopen("DATAinput.txt", "a");    
    printf("Enter album artist \n");    
    scanf("%s", albA);    
    fprintf(fileD, "%s ", albA); 

    // add other scanf and fprintfs         
    fclose(fileD);    

} 

void LoadData(void)
{
    LEL *ptr;
    FILE * file;
    file = fopen("DATA.txt", "r");
    if (file == NULL) {
        printf("Can not open the file\n");
    }
    while(fscanf (file, "%s %s %s %s %s", ptr->inform.index, ptr->inform.name, ptr->inform.release, ptr->inform.length, ptr->inform.artist) == 5)
    {
        ptr = malloc(sizeof(LEL));
        ptr->next=NULL;
        printf("\n %s Album: %s  Released: %s  Length: %s  Artist: %s", ptr->inform.index, ptr->inform.name, ptr->inform.release, ptr->inform.length, ptr->inform.artist);
        if (head == NULL)
        {
           head = ptr;
           current = ptr;
        }
        else
        {
           current ->next = ptr;
           current = current->next;
        }
    }
    fclose(file);

}

void OutpuAll(void) // Trying to output data that i previously loaded from files to structure.    
{    
    LEL *ptr; 
    LEL* ptr = head;
    while (ptr!=NULL) {  
        printf("\n Id:%s Album: %s  Released: %s  Length: %s  Artist: %s", ptr->inform.index, ptr->inform.name, ptr->inform.release, ptr->inform.length, ptr->inform.artist);
        ptr = ptr->next;
    }     
}
Related