Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Online Systems For Physicians And Medical Professionals How To Use And Access Databases

Authors: Harley Bjelland

1st Edition

1878487442, 9781878487445

More Books

Students also viewed these Databases questions

Question

=+2 Why are so many countries bothered by their brain drains?

Answered: 1 week ago