Question: write a routine free room to remove a guest from his or her room. Then write add guest to place a new guest into a

write a routine free room to remove a guest from his or her room. Then write add guest to place a new guest into a room, checking first whether the room is empty. incorporate the getoccupier function and if necessary modify it. Also, find a way to calculate the room number of each guest. The name below should be place in a file with a name each line.

John Smith Susan Taylor Edward Doe Richard Lee Jane Smith 

#include #include #include

#define NAMELENGTH 41

char namebuf[NAMELENGTH]; //buffer to hold name int infile=-1; //will hold file descriptor

char *getoccupier(int roomno) { off_t offset; ssize_t nread;

//open the file first time around if (infile==-1 && (infile=open("residents", O_RDONLY))==-1) { return (NULL); //couldnt open file } offset=(roomno-1)*NAMELENGTH; //find room slot and read occupier's name if(lseek(infile, offset, SEEK_SET)==-1) return (NULL); if((nread=read(infile, namebuf, NAMELENGTH))<=0) return(NULL);

//create a string by replacing the newline character with the null terminator namebuf(nread-1)='\0'; return (namebuf);

} void addroom(char*,int)//checks whether a room is empty, if so places new guest there else error message and exit #define NROOMS 10

int main() {

int j; char* getoccupier(int), *p; int findroom(), availRoomNo; void freeroom(int); void addroom(char*, int);

for(j=1; j<=NROOMS; j++) { if(p=getoccupier(j)) printf("Room %2d, %s ", j, p); else printf("Error on room %d ", j); } availRoom = findroom(); if(availRoom >0) addroom("Richard Smith", availRoom); availRoom = findroom(); if(availRoom >0) addroom("Paul Doe", availRoom); freeroom(5); availRoom = findroom(); if(availRoom >0) addroom("Anne Smith", availRoom); printf("After changes, room occupants are: "); for(j=1; j<=NROOMS; j++) { if(p=getoccupier(j)) printf("Room %2d, %s ", j, p); else printf("Error on room %d ", j); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!