Question

student programmer here. I am having trouble reading input from a binary file in C. The data stored in the file are structs that look like this:

typedef struct reserve{
    char *name;
    char *ic;
    int room_number;
} Reserve;

I get no problem when I write the struct to a binary file. Here is the code I did for writing to the file:

void reg_new() //Make a new reservation
{
    Reserve newReserve = {"", "", 0};
    char name[NAMEFIELD], ic[ICFIELD];
    int room_number;
    FILE *resvptr;

    scanf_s("%s", name, NAMEFIELD);
    scanf_s("%s", ic, ICFIELD);
    scanf_s("%d", &room_number);

    newReserve.name = name;
    newReserve.ic = ic;
    newReserve.room_number=room_number;

resvptr = fopen("reservations.dat", "wb");
fwrite(&newReserve, sizeof(Reserve), 1, resvptr);
fclose(resvptr);

}

And the code for reading from the file:

void reg_view() //view a reservation.
{
FILE *seekptr;
Reserve viewReserve = {"", "", 0};
int read;

if ( (seekptr = fopen("reservations.dat", "rb")) == NULL)
{
    puts("Error: file could not be found.");
}
else
{
    while ( !feof(seekptr))
    {
        read = fread(&viewReserve, sizeof(Reserve), 1, seekptr);

        if(read != 0)
        {
        printf("Name: %s\nIC number: %s\nRoom Number: %d\n", viewReserve.name, viewReserve.ic, viewReserve.room_number);
        }
    }
    fclose(seekptr);
}

Immediately after writing to the binary file and accessing the function for reading, the room number (of int value) reads fine, but the two strings (name and IC number) read out as garbage characters. Subsequently, closing and restarting the program, then attempting to read from the file will give me an access violation and bad pointer error.

The trouble is with reading the strings from the file. I'm suspecting a memory leak but I can't say for sure...can anyone help? Thanks.

Was it helpful?

Solution

You are writing pointers to the file, not the actual data. When you read the pointer from the file, it's just a memory address that you probably don't have access to, even if you do it in the same process that wrote the file (because it was actually a pointer to a function-scoped stack variable).

If you want to store the records in that way, make your function store the actual string data rather than the pointer:

typedef struct reserve{
    char name[NAMEFIELD];
    char ic[ICFIELD];
    int room_number;
} Reserve;

And just do this:

scanf_s("%s", newReserve.name, NAMEFIELD);
scanf_s("%s", newReserve.ic, ICFIELD);
scanf_s("%d", &newReserve.room_number);

OTHER TIPS

The structure consists of the integer and the addresses of two arrays (pointers). When you write it out and read it back, you are reading and writing the addresses of the character arrays - not their contents. You need to modify your routine to write and read the contents (including lengths) and also to allocate the storage to hold the contents when you read them back in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top