fwriting and freading binary (structs)
Here is my struct:
Code:
struct location {
int houseNumber;
char streetName[STRING_BUF];
char city[STRING_BUF];
char state[STATE_BUF];
int zip;
};
struct person {
char nameFirst[STRING_BUF];
char nameLast[STRING_BUF];
char number[STRING_BUF];
struct location address;
struct person *next;
} entry[100];
typedef struct person PERSON;
typedef PERSON *PERSON_PTR;
And here is the fwrite:
Code:
void write_to_disk (PERSON_PTR first)
{
PERSON_PTR cur_ptr;
cur_ptr = first;
if ( (fp = fopen(filename, "wb") ) == NULL )
{
fprintf(stderr, "Error opening file %s\n", filename);
exit(1);
}
while (cur_ptr != NULL)
{
if ( fwrite(cur_ptr, sizeof(PERSON), 1, fp) != 1)
{
fprintf(stderr, "Error writing to %s.", filename);
exit(1);
}
cur_ptr = cur_ptr->next;
}
fclose(fp);
}
But now how to read the file? I can post my attempt but wanted to keep it streamline. I actually don't even know if I'm writing to the file correctly.