2

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://i.sstatic.net/QJIYA.jpg

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
  • Your output is plain text. Please don't add pictures of text. Instead add the text into the question body. Also the error message could easily be added as text. Commented Jun 27, 2019 at 8:36

1 Answer 1

1

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;
    }     
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, i didnt wrote all scanfs because they large and pretty identical, i wanted to minimize code here . Thank you, i will correct my errors.
I tried to use your corrections, but its crashes again. Can you, please, help me. This is my entire code with your corrections - pastebin.com/8rKrmPs4 . The main problem - i loading data from DATA file and i need put this data in to to linked list structure, so i can call whole data massive with one function and get full list with albums that i loaded from the DATA and albums that i added with console from DATAinput. That the main problem, i cant find the way to move data in to the structure because i have troubles with pointers and allocating dynamic memory, i tried many-many times.
I have added all corrections to my code but it still doesn't work. It enters in the while cycle and crushes. I think problem somewhere in while condition (fscanf (file, "%s ...... ptr->inform.artist) == 5) i tried using =!EOF and "a" mode but nothing seems to help. Here is the updated code with the latest corrections and i deleted everything excess pastebin.com/QkjBSmmC . Also i want to mention, that my list is SINGLY linked, so i suppose we can have only one pointer - *next ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.