0

I tried to read the binary file into an array of structure. There will be a total of the records in the file at the beginning of the file and followed by all the records.

My plan is to read the file into a structure according to the total of records, use malloc enlarge the structure size, add more records into it, and overwrite the total of records and structure into the file.

void addRecord() {
    Staff* staff_data;
    int total;
    staff_data = read_data("staff_info.bin", &total);

    if (staff_data == NULL)
    {
        printf("Error reading from file.\n");
        return 1;
    }

    staff_data = malloc(sizeof(Staff) * total + 1);

    printf("\n>>>>> Enter New Employee Details <<<<<\n\n");
    printf("Employee ID                : %d\n", staff_data[total-1].id +1);
    printf("Name                       : ");
    rewind(stdin);
    gets(staff_data[total].name);
    printf("Position                   :");
    rewind(stdin);
    gets(staff_data[total].position);
    printf("Phone Number               :");
    rewind(stdin);
    gets(staff_data[total].phoneNo);
    printf("Password                   :");
    rewind(stdin);
    gets(staff_data[total].password);
    printf("Date of Birth (DD-MM-YYYY) : ");
    scanf("%d-%d-%d", &staff_data[total].dob.day, &staff_data[total].dob.month, &staff_data[total].dob.year);
    printf("Date Joined (DD-MM-YYYY)   : ");
    scanf("%d-%d-%d", &staff_data[total].dateJoined.day, &staff_data[total].dateJoined.month, &staff_data[total].dateJoined.year);
    printf("Salary                : ");
    scanf("%lf", &staff_data[total].salary);
    printf("%s", staff_data[total].name);
    total++;

    if (write_data("staff_info.bin", staff_data, total))
        printf("Data Written Succesfully.\n");
    else
        printf("Error writing the file.\n");
}


Staff* read_data(char* filename, int* total)
{
    FILE* file;
    file = fopen(filename, "rb");

    if (file == NULL) return NULL;

    if (fread(total, sizeof(int), 1, file) != 1) return NULL;

    Staff* data = malloc(sizeof(Staff) * *total);

    if (fread(data, sizeof(Staff), *total, file) != *total)
    {
        free(data);
        return NULL;
    }

    if (fclose(file) == EOF)
    {
        free(data);
        return NULL;
    }

    return data;
}

bool write_data(char* filename, Staff* data, int total) {
    FILE* file;

    file = fopen(filename, "wb");

    if (file == NULL) return false;

    // If fwrite got an error, return false
    if (fwrite(&total, sizeof(int), 1, file) != 1)
        return false;

    // If fwrite doesnt equal total, return false
    if (fwrite(data, sizeof(Staff), total, file) != total)
        return false;

    // If fclose doesnt work, return false
    if (fclose(file) == EOF) return false;

    // Everything works well, return true
    return true;
}

Im new to programming, I followed a video on youtube. The 2 functions are from the video but I dont really understand the code because I just learned the concept of pointer.

After reading the file, I found that when I tried to print the id of staff from the structure array, some random number will show up. Output:

>>>>> Enter New Employee Details <<<<<

Employee ID                : -842150450

The same thing happens when I tried to print the name and other data, there are some unknown symbols instead of a readable string.

═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════²²²²▌▌▌p╒¶╓╫m
  • 1
    `staff_data = read_data("staff_info.bin", &total);` then `staff_data = malloc(..)` is not what you want. At best you're creating a memory leak by overwriting the pointer returned from `read_data`. If you want to expand your memory chunk, use [`realloc`](https://linux.die.net/man/3/realloc) instead – yano Mar 07 '22 at 15:46
  • 1
    [Never use `gets`](https://stackoverflow.com/q/1694036/2505965). – Oka Mar 07 '22 at 21:57

0 Answers0