Searching and Updating a File

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2008
Posts: 88
Reputation: knight fyre is an unknown quantity at this point 
Solved Threads: 1
knight fyre's Avatar
knight fyre knight fyre is offline Offline
Junior Poster in Training

Searching and Updating a File

 
0
  #1
Mar 29th, 2008
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.

  1. // ROOM ASSIGNMENT FUNCTION
  2. int room_assigner(int type, int status) // type (1- smoking 2- non-smoking); status (9-reserve 10-vacant 12-occupied)
  3. {
  4. FILE *roomPtr;
  5. int r_num = 0; // room number
  6. int seeker = 1; // use to traverse through each record
  7.  
  8. if((roomPtr = fopen("rooms.txt", "rb+")) == NULL)
  9. {
  10. printf("\nError in opening file");
  11. }
  12. else
  13. {
  14. do
  15. { // if seeker = 1 then record 1 field room_status should be checked by the if function
  16. fseek( roomPtr, ( seeker - 1) * sizeof( ROOM_DATA ), SEEK_SET);
  17. // read the info. from the file
  18. fread( &room, sizeof( ROOM_DATA ), 1, roomPtr );
  19.  
  20. if( room.room_status == 10 )
  21. {
  22. if( type == 1 ) // smoking
  23. {
  24. room.room_rate = 4000;
  25. }
  26. else // non-smoking
  27. {
  28. room.room_rate = 3000;
  29. }
  30. room.room_num = room.room_num;
  31. room.room_type = room.room_type;
  32. room.room_status = status;
  33. r_num = room.room_num; // assigns the chosen room
  34. fwrite( &room, sizeof( ROOM_DATA ), 1, roomPtr );
  35. }
  36. else
  37. {
  38. seeker++;
  39. }
  40. }while(room.room_status != 10 && !feof(roomPtr)); // loop ends only if a empty room was found or the end of the file is reached
  41. }
  42.  
  43. return r_num; // return the choosen room, if zero then a room was not found
  44. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Searching and Updating a File

 
0
  #2
Mar 29th, 2008
line 32 sets status to something other than 10. That means its value is not 10 when line 40 is executed, so the loop continues.

I would use a different variable, such as bool found = false then set it to true when line 30 is executed.
  1. bool found = false;
  2. do
  3. {
  4. <snip>
  5. found = true;
  6. // line 30 here
  7. <snip>
  8. } while( found == false && !feof() );
Last edited by Ancient Dragon; Mar 29th, 2008 at 12:35 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 88
Reputation: knight fyre is an unknown quantity at this point 
Solved Threads: 1
knight fyre's Avatar
knight fyre knight fyre is offline Offline
Junior Poster in Training

Re: Searching and Updating a File

 
0
  #3
Mar 29th, 2008
I get the following errors with bool and it still doesn't work when I include <stdbool.h>...

Error: c-project_.c(313,14):Undefined symbol 'bool'
Error: c-project_.c(313,14)tatement missing ;
Error: c-project_.c(329,18):Undefined symbol 'found'
Error: c-project_.c(329,24):Undefined symbol 'true'
Error: c-project_.c(348,28):Undefined symbol 'false'
Error: c-project_.c(7,2):Unable to open include file 'stdbool.h'

I resorted to

  1. int found = 0;
  2. do
  3. {
  4. <snip>
  5. found = 1;
  6. // line 30 here
  7. <snip>
  8. } while( found == 0 && !feof(roomPtr) );

but the I ave another problem; line 32 doesn't work. When the data is being written back to the file. The status is suppose to change to reserved (or the integer 9) but when I check the file, available (or the integer 10) is still in it's place.
Last edited by knight fyre; Mar 29th, 2008 at 1:03 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Searching and Updating a File

 
0
  #4
Mar 29th, 2008
Oops! just define it as an int instead of a bool. bool is defined in c++, and I think the newest version of C too.
  1. #define TRUE 1
  2. #define FALSE 0
  3.  
  4. int done = FALSE;
  5. do
  6. {
  7. <snip>
  8. done = TRUE;
  9. } while( done == FALSE && ...);

>>Error: c-project_.c(7,2):Unable to open include file 'stdbool.h'

Is there such a file?
Last edited by Ancient Dragon; Mar 29th, 2008 at 1:04 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Searching and Updating a File

 
0
  #5
Mar 29th, 2008
bool is an afterthought of a boolean type for c added in C99 but it doesn't exit in C89. A macro BOOL exist for C89 but... no need.

  1. #define FALSE 0
  2. #define TRUE (!FALSE)
  3.  
  4. int found = FALSE;
  5.  
  6. and inside the loop
  7. found = TRUE;
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 88
Reputation: knight fyre is an unknown quantity at this point 
Solved Threads: 1
knight fyre's Avatar
knight fyre knight fyre is offline Offline
Junior Poster in Training

Re: Searching and Updating a File

 
0
  #6
Mar 29th, 2008
That's what it saw in Deitel and Deitel C How to Program. Said C99 provides the header file.

I have another problem; line 32 doesn't work. When the data is being written back to the file. The status is suppose to change to reserved (or the integer 9) but when I check the file, I see available (or the integer 10) is still in it's place.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Searching and Updating a File

 
0
  #7
Mar 29th, 2008
Originally Posted by Ancient Dragon View Post
>>Error: c-project_.c(7,2):Unable to open include file 'stdbool.h'

Is there such a file?
Yes, but it is added in the C99 standard.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC