| | |
Searching and Updating a File
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
c Syntax (Toggle Plain Text)
// 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 }
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
I would use a different variable, such as
bool found = false then set it to true when line 30 is executed. C Syntax (Toggle Plain Text)
bool found = false; do { <snip> found = true; // line 30 here <snip> } 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.
I get the following errors with bool and it still doesn't work when I include <stdbool.h>...
I resorted to
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.
•
•
•
•
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
c Syntax (Toggle Plain Text)
int found = 0; do { <snip> found = 1; // line 30 here <snip> } 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.
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.
>>Error: c-project_.c(7,2):Unable to open include file 'stdbool.h'
Is there such a file?
C Syntax (Toggle Plain Text)
#define TRUE 1 #define FALSE 0 int done = FALSE; do { <snip> done = TRUE; } 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.
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.
C Syntax (Toggle Plain Text)
#define FALSE 0 #define TRUE (!FALSE) int found = FALSE; and inside the loop found = TRUE;
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.
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.
![]() |
Similar Threads
- new win32 (Viruses, Spyware and other Nasties)
- Vista Search question (Windows Vista and Windows 7)
- Getting errors while compiling (Java)
- I NEED HELP PLEASE:Warning: mysql_num_rows(): (PHP)
- I NEED HELP PLEASE:Warning: mysql_num_rows(): (MySQL)
- Warning: mysql_num_rows(): (PHP)
- searching booth and rundll errors (Viruses, Spyware and other Nasties)
- Searching in documents... (C++)
- 35 processes, need to trim the fat (Viruses, Spyware and other Nasties)
Other Threads in the C Forum
- Previous Thread: Random Files
- Next Thread: read to end of line problem
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches infiniteloop initialization intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test testautomation threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi







tatement missing ;