I'm trying to create a function that searches through all the beds in a hotel until it finds one that is available or just ends when all the records are searched. When it finds that bed, it updates that bed record with new information (marking it as reserved or occupied). The problem is that my function keeps on looping just above the fseek. I know this because I placed a simple print statement above it. At the end of it's run the function is suppose to return the room number that's available to the caller.
// ROOM ASSIGNMENT FUNCTION
int room_assigner(int type, int status) // type (1- smoking 2- non-smoking); status (9-reserve 10-vacant 12-occupied)
{
FILE *roomPtr;
int r_num = 0; // room number
int seeker = 1; // use to traverse through each record
if((roomPtr = fopen("rooms.txt", "rb+")) == NULL)
{
printf("\nError in opening file");
}
else
{
do
{ // if seeker = 1 then record 1 field room_status should be checked by the if function
fseek( roomPtr, ( seeker - 1) * sizeof( ROOM_DATA ), SEEK_SET);
// read the info. from the file
fread( &room, sizeof( ROOM_DATA ), 1, roomPtr );
if( room.room_status == 10 )
{
if( type == 1 ) // smoking
{
room.room_rate = 4000;
}
else // non-smoking
{
room.room_rate = 3000;
}
room.room_num = room.room_num;
room.room_type = room.room_type;
room.room_status = status;
r_num = room.room_num; // assigns the chosen room
fwrite( &room, sizeof( ROOM_DATA ), 1, roomPtr );
}
else
{
seeker++;
}
}while(room.room_status != 10 && !feof(roomPtr)); // loop ends only if a empty room was found or the end of the file is reached
}
return r_num; // return the choosen room, if zero then a room was not found
}