Home > front end >  Incorrect entry in file from linked list
Incorrect entry in file from linked list

Time:05-27

This is the one of main part of the code that the pls format file should convert to m3u format song under numbers of seconds which specified :

[playlist]
NumberOfEntries=3
File1=C:\muzika\Chris Rea - Looking For The Summer.mp3
Title1=Chris Rea - Looking For The Summer
Length1=302
File2=C:\muzika\Simply Red - Holding Back The Years.mp3
Title2=Simply Red - Holding Back The Years
Length2=265
File3=C:\muzika\Mambo Kings - Luz de luna.mp3
Title3=Mambo Kings - Luz de luna
Length3=207
Version=2

convert to:

#EXTINF:207,Mambo Kings - Luz de luna
C:\muzika\Mambo Kings - Luz de luna.mp3
#EXTINF:265,Simply Red - Holding Back The Years
C:\muzika\Simply Red - Holding Back The Years.mp3

.

if ( file != NULL )
{
    char line[256];
    while (fgets(line, sizeof line, file) != NULL)
    {
        if (count == lineNumber)
        {
            int j=0;
            if (line[0] == 'F') {
                int brojac=6;
                while(line[brojac]!='\n'){
                    ffolder[j]=line[brojac];
                    j  ;
                    brojac  ;
                }
                folder=ffolder;



            }
            if (line[0] == 'T') {
                int brojac=7;
                while(line[brojac]!='\n'){
                    naslov1[j]=line[brojac];
                    j  ;
                    brojac  ;
                }
                naslov=naslov1;



            }
            if (line[0] == 'L') {
                int brojac=8;
                while(line[brojac]!='\n'){
                    vremee[j]=line[brojac];
                    j  ;
                    brojac  ;
                }
                vreme=vremee;
                //intvreme = folder11(line);
                if(atoi(vremee)<atoi(argv[3])) {
                    //fprintf(out, "\n#EXTINF:%s,%s\n%s", vremee,naslov1,ffolder);**key part**
                    struct pesma *link = (struct pesma *) malloc(sizeof(struct pesma));
                    link->folder = folder;
                    printf("%s\n",folder);
                    link->naslov = naslov;
                    printf("%s\n",naslov);
                    link->vreme = atoi(vreme);
                    link->next = NULL;
                    if (!glava) {
                        glava = link;
                    } else {
                        struct pesma *tail = glava;
                        while (tail->next) {
                            tail = tail->next;
                        }
                        tail->next = link;
                    }
                }
            }




        }

        else
        {
            count  ;
        }
    }

}

When I call the function that should print the elements of the linked list, for each song that meets the criteria, print this:

#EXTINF:207,Mambo Kings - Luz de luna
C:\muzika\Mambo Kings - Luz de luna.mp3
#EXTINF:265,Mambo Kings - Luz de luna
C:\muzika\Mambo Kings - Luz de luna.mp3

vreme is normal,but folder and naslov are from last last line in file.

However, if I use the fprintf function (in the key part in the main code) instead of a concatenated list, the printout is correct, but then I can't sort alphabetically.

fprintf(out, "\n#EXTINF:%s,%s\n%s", vremee,naslov1,ffolder);

I'm wondering why the linked list is printed like this?

CodePudding user response:

my guess - cant do better since we have an incomplete program

link->folder = folder;

link is a char * member. If so you should do

link->folder = strdup(folder);

otherwise they all end up pointing to the buffer 'folder' and whatever it last contained

CodePudding user response:

Your application modified so that it reads the input data into a linked list. Using char arrays in the song struct makes them easier to handle. For sorting the list you can look at sorting linked list simplest way. Convert the song data into your target format should now be a piece of cake.

There remains some work to do with this code:

  • Extract duplicate code into functions
  • Complete error handling, a.o. check all function return values
  • Maybe use a command line argument for the input file name

In general, try to use English words for names (data structures, functions, variables, etc.).

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

struct song { // pesma
    char file[256];
    char title[256]; // naslov
    int time; // vreme
    struct song * next;
};

int main()
{
    char line[256];
    char *filename = "playlist.txt";

    // pointer to first element of the linked song list
    struct song *root = NULL; // glava

    FILE *file = fopen(filename, "r");

    if (file == NULL) {
        fprintf(stderr, "Could not open file '%s'!\n", filename);
        return EXIT_FAILURE;
    }

    // read line containing "[playlist]"
    fgets(line, sizeof line, file);

    // read line containing "NumberOfEntries=3"
    fgets(line, sizeof line, file);
    char *start_position = strchr(line, '=')   1;
    int number_of_entries = atoi(start_position);
    printf("Number of entries: %d\n", number_of_entries);

    for (int i = 0; i < number_of_entries; i  ) {
        // allocate new node for linked list and put it to the front of the list
        struct song *node = (struct song *)malloc(sizeof(struct song));
        node->next = root;
        root = node;

        // read file name
        if (fgets(line, sizeof line, file) == NULL) {
            fprintf(stderr, "Unexpected end of file!\n");
            fclose(file);
            // TODO: release allocated memory
            return EXIT_FAILURE;
        }
        start_position = strchr(line, '=')   1;
        strcpy(node->file, start_position);
        node->file[strlen(start_position) - 1] = '\0';

        // read title
        if (fgets(line, sizeof line, file) == NULL) {
            fprintf(stderr, "Unexpected end of file!\n");
            fclose(file);
            // TODO: release allocated memory
            return EXIT_FAILURE;
        }
        start_position = strchr(line, '=')   1;
        strcpy(node->title, start_position);
        node->title[strlen(start_position) - 1] = '\0';

        // read time
        if (fgets(line, sizeof line, file) == NULL) {
            fprintf(stderr, "Unexpected end of file!\n");
            fclose(file);
            // TODO: release allocated memory
            return EXIT_FAILURE;
        }
        start_position = strchr(line, '=')   1;
        int time = atoi(start_position);
        node->time = time;
    }

    // read version
    if (fgets(line, sizeof line, file) == NULL) {
        fprintf(stderr, "Unexpected end of file!\n");
        fclose(file);
        // TODO: release allocated memory
        return EXIT_FAILURE;
    }
    start_position = strchr(line, '=')   1;
    int version = atoi(start_position);
    printf("Version: %d\n", version);

    // just print all songs
    struct song * iterator = root;
    while (iterator != NULL) {
        printf("File:  %s\n", iterator->file);
        printf("Title: %s\n", iterator->title);
        printf("Time:  %d\n", iterator->time);

        iterator = iterator->next;
    }

    // free the allocated memory
    while (root != NULL) {
        struct song * next = root->next;
        free(root);
        root = next;
    }

    fclose(file);

    return EXIT_SUCCESS;
}
$ gcc -Wall convert_playlist.c
$ ./a.out                     
Number of entries: 3
Version: 2
File:  C:\muzika\Mambo Kings - Luz de luna.mp3
Title: Mambo Kings - Luz de luna
Time:  207
File:  C:\muzika\Simply Red - Holding Back The Years.mp3
Title: Simply Red - Holding Back The Years
Time:  265
File:  C:\muzika\Chris Rea - Looking For The Summer.mp3
Title: Chris Rea - Looking For The Summer
Time:  302
$ 
  • Related