Search code examples
clistbinaryfiles

Program displays random lists data when reading from file


I have a homework task to create a linked list with data about excursions and write the data in a binary file and then read it.But when I write a function to display all lists, it displays the lists I have created but also shows random data.

I have tried using different loops but for some reason with for loop it doesn't display anything, just crashes. I am a beginner in C so I am sorry if the question is too stupid... :D

typedef struct {
    char ID[20];
    char date[11];
    int duration;
    double price;
} excursion;

typedef struct Trip {
    excursion data;
    struct Trip *next;
} trip;


trip *head=NULL;
trip *current=NULL;


void displayALL()
{
        trip *temp;

        temp = head;
        while (temp != NULL) {
                printf("ID of Excursion is %s\nDuration is %d days\nDate of departure is %s\nThe price is %.2f\n",
                                temp->data.ID, temp->data.duration, temp->data.date, temp->data.price);
                temp = temp->next;
        }
}

I won't be showing the entire code because the other part works I write the lists with this code:

FILE * fp;
trip *temp;

if ((fp = fopen("Excursion.bin", "wb")) == NULL) {
        printf("Error opening file");
        exit(1);
}

for (temp = head; temp != NULL; temp = temp->next) {
        if (fwrite(&temp->data, sizeof(excursion), 1, fp) != 1) {
                printf("Error in writing file\n");
                exit(0);
        }
}
fclose(fp);

and read with this one :

FILE *fp;

if ((fp = fopen("Excursion.bin", "rb")) == NULL) {
        printf("No info added yet\n");
        exit(1);
}
while (1) {
        trip *temp = (trip*)malloc(sizeof(trip));
        if (head == NULL) {
                head = temp;
                current = head;
                current->next = NULL;
        } else {
                current->next = temp;
                current=temp;
                current->next = NULL;
        }
        if (fread(&temp->data, sizeof(excursion), 1, fp) != 1) {
                break;
                printf("Error reading file\n");
                exit(0);
        }
}
fclose(fp);

This is the random data it displays: ID of Excursion is └ Duration is 0 days Date of departure is The price is 0.00 ID of Excursion is И#▌ Duration is -202182160 days Date of departure is фхцчшщъыьэюяЁёЄєЇїЎў°∙·√№¤■ а5▐ The price is -1.#R


Solution

  • Your main issues are right here.

    if(fread(&temp->data, sizeof(excursion), 1, fp) != 1)
    

    And here

    if(fwrite(&temp->data,sizeof(excursion), 1, fp) != 1)
    

    So it appears you're trying to write an entire structure to your file and read the entire structure but for some reason you're telling it to put it in data, or take it out of data. Data is not is the whole structure it's 11 byte character array inside the structure.

    Do this.

    if(fread(temp, sizeof(excursion), 1, fp) != 1)
    

    And

     if(fwrite(temp,sizeof(excursion), 1, fp) != 1)