I am reading and writing a struct to/from a random access file. File pointer is defined in the main function and passed to other functions. I tried to put fflush
everywhere that I can yet I still get this error. I don't know what I am missing.
typedef struct seatdata
{
int seat_num;
char name[15];
char surname[15];
char gender;
}Seatdata;
...
void newReservation( FILE* data )
{
Seatdata buffer;
int seat_number;
printf( "Enter seat number to be reserved: " );
scanf( "%d", &seat_number );
fseek( data, (seat_number-1)*sizeof(Seatdata), SEEK_SET );
fread( &buffer, sizeof(Seatdata), 1, data );
fflush( data );
if ( buffer.seat_num != 0 )
{
printf( "This seat has already been reserved." );
return;
}
printf("Enter name surname and gender: " );
scanf("%s %s %c", buffer.name, buffer.surname, &buffer.gender );
fwrite( &buffer, sizeof(Seatdata), 1, data );
fflush( data );
}
...
int main ( void )
{
FILE* data = NULL;
data = fopen( "bus.dat", "rb+" );
if ( data == NULL ) { return -1; }
newReservation( data );
return 0;
}
Error comes at the fwrite
part.
And apparently I am required to pass the FILE* to functions. ( This is an assignment )