0

I'm having trouble getting to understand why my code is not working as intended, and I'm sure it has to do with my understanding of fread and fwrite.

Here is a snippet of my code, excuse the formatting and lazy style.

typedef struct record_t{
char name[20];
unsigned int id;
char toy[30];
}record_t;



int main(int argc, char** argv){


FILE *readFile, *writeFile;
record_t *recordPtr; 
int i;

if( (recordPtr = malloc(sizeof(struct record_t))) == NULL)
{
    perror("Error allocating record space\n");
    exit(EXIT_FAILURE);
}

strcpy(recordPtr->name, "Michael"); 
recordPtr->id = 3;
strcpy(recordPtr->toy, "Hello Kitty");
printf("Writing %s - %d - %d - %d - %s\n", recordPtr->name,
    recordPtr->id, recordPtr->toy);

if( (writeFile = fopen("test.bin", "wb")) == NULL)
{
    perror("Error opening writing file\n");
    exit(EXIT_FAILURE);
}

for(i = 0; i < 2; ++i){
recordPtr->id = i ;
fwrite(&recordPtr, sizeof(struct record_t), 1, writeFile);
printf("%s - %d - %s, ", recordPtr->name, recordPtr->id, recordPtr->toy);
printf("Record Written.\n");

}
fclose(writeFile);

recordPtr = NULL;

if( (readFile = fopen("test.bin", "rb")) == NULL)
{
    perror("Error opening reading file\n");
}
    exit(EXIT_FAILURE);

recordPtr = NULL;


for(i=0; i < 2; ++i){
    fread(&recordPtr, sizeof(struct record_t), 1, readFile);
    printf("%s - %d - %s, Read back.\n" recordPtr->name, recordPtr->id, recordPtr->toy);
}
fclose(readFile);


exit(EXIT_SUCCESS);
}

The idea of the code is just to take a record, write it to binary, then read it back from binary. My issue is that only the last entry is read back, so I'm sure I have a problem with my understanding of fread.

Thank-you in advance.

husked
  • 3
  • 3
  • Are you saying that when it goes through the read loop, it prints id 2 all three times, instead of 0, 1, 2? – Barmar Apr 14 '13 at 02:47
  • I think the issue is that the record being written is the same record, over and over again. so rather than seeing record 0 then 1 then 2 etc im seeing the last record written 1, then 1, then 1. – husked Apr 14 '13 at 03:07
  • Have you read the answer that was posted 20 minutes ago? – Barmar Apr 14 '13 at 03:09

2 Answers2

1

This

fwrite(&recordPtr, sizeof(struct record_t), 1, writeFile);

isn't writing the structure to the file, it's attempting to write the pointer recordPtr to it followed by whatever follows it in memory.

Likewise this

fread(&recordPtr, sizeof(struct record_t), 1, readFile);

isn't reading the structure into the memory you've allocated for it, it's attempting to overwrite the pointer recordPtr and whatever follows it in memory with file data.

You need to drop that ampersand.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
1
fwrite(&recordPtr, sizeof(struct record_t), 1, writeFile);
fread(&recordPtr, sizeof(struct record_t), 1, readFile);

should be:

fwrite(recordPtr, sizeof(struct record_t), 1, writeFile);
fread(recordPtr, sizeof(struct record_t), 1, readFile);

Next:

if( (readFile = fopen("test.bin", "rb")) == NULL)
{
    perror("Error opening reading file\n");
}
exit(EXIT_FAILURE);

Should be

if( (readFile = fopen("test.bin", "rb")) == NULL)
{
    perror("Error opening reading file\n");
    exit(EXIT_FAILURE);
}

And

when you are reading the structure back, the buffer is pointing to NULL!!! So make sure you allocate memory and make recordPtr point to it before you use recordPtr in fread.

codaddict
  • 445,704
  • 82
  • 492
  • 529
  • This answer solves my issue regarding fread, but after amending this when reading back i'm getting nothing back. So there are more issues with how I'm reading in the record. Thank-you. – husked Apr 14 '13 at 03:22