Vote count:
0
I have a file called RESERVED.dat and i'm adding room numbers to the file. However if a certain room number has already been entered and i try to enter that same number again i want the program to printf that this room has already been entered. The program works partially, if for e.g. I enter 1 as a room number when the file is empty it first says that the room is currently empty and it is added to the file, so it works properly. If i enter room 2 on a second run of the program it works how it should. And it goes on working properly ONCE i keep entering room #s consecutively in this ascending order.
But here's the problem: Say I enter 3 when RESERVED.dat is empty, it says room 3 is empty and does whatever is in the else {} (which its supposed to do). Then if i go back again and enter 3 it still says room 3 is empty and does the else{} but its not supposed to(it continues saying this until i enter 3 four times in a row, then it finally says room 3 is occupied). But if i enter any number less than 3 e.g(I enter 2) it says room 3 is already occupied thereby, doing the if{} block (when its supposed to be saying room 2 is empty and adding it). Its like it saves all the numbers before the number i actually enter.
main() only contains a switch statement giving options inclusive of this function.
I HAVE TRIED ABSOLUTELY EVERYTHING TO GET THIS WORKING.
struct SuiteRooms {
int roomNo;
int occupancy;
};
void addAdult(){
//struct adltGuests adlt;
struct SuiteRooms suiterooms = {0,0};
int room;
int i;
ftaken = fopen("RESERVED.dat","rb");
printf("Enter a room number (1 - 90)");
scanf("%d",&room);
fseek(ftaken,(room - 1) * sizeof(struct SuiteRooms),SEEK_SET);
fread(&suiterooms, sizeof(struct SuiteRooms), 1, ftaken);
printf("suiterooms.roomNo is %d\n", suiterooms.roomNo);
if(suiterooms.roomNo!=0){
printf("Room %d already occupied\n", suiterooms.roomNo);
system("pause");
fclose(ftaken);
menuLoop++;
}
else{
ftaken = fopen("RESERVED.dat","ab");
printf("Room %d is empty\n", suiterooms.roomNo);
suiterooms.roomNo = room;
fseek(ftaken,(suiterooms.roomNo - 1) * sizeof(struct SuiteRooms), SEEK_SET);
fwrite(&suiterooms, sizeof(struct SuiteRooms), 1, ftaken);
system("pause");
menuLoop++;
}
fclose(ftaken);
}// End of function addAdult
Logic Errors in file manipulation c programming
Aucun commentaire:
Enregistrer un commentaire