Question: just add the following functions to a class before main. program is in C. above is getoccupier function. Given a text file, residents.txt, uploaded into
just add the following functions to a class before main. program is in C. above is getoccupier function.
Given a text file, residents.txt, uploaded into the assignment folder, create a program with the following functionality. You should start from the getoccupier program in p. 24-25 of the textbook.
- Create and add the following function to locate the lowest numbered free room. If every room is occupied, then it should return 0.
- int findfree()
- Create and add the following function to remove a guest from his/her room.
- void freeroom(int roomno)
- Create and add the following function to place a new guest into a room. You have to first check whether the room is empty first. If it is not empty, then it you print an error message and exit.
- void addroom(char* name, int roomno)
- Use the given main() function to compile and run your program.
- Use the given residents.txt file as the file containing occupants information.
- You also need to include the definition of the getoccupier() function into your program since the given main() will make use of that, too.
The residents.txt file is formatted exactly according to the text book description as in the hotel example. 41 bytes are used for each room occupant. A 41-byte record corresponding to an empty room contains 40 empty space characters followed by a new line ( ) characte.
#define NROOMS 10 int main() { int j; char* getoccupier(int), *p; int findroom(), availRoomNo; void freeroom(int); void addroom(char*, int); for(j=1; j0) 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 John Smith Susan Taylor Edward Doe Richard Lee Jane Smith
Bb Home Page - COS X Bb Unix System Progr x PDF Chap 1-3.pdf Bb https://learn-us-e X Bb https://learn-us-ea X C Just These Functio X M Inbox (284) - fekac X + File C:/Users/fiktiru/Documents/Chap 1-3.pdf To w o + : oo 17 of 36 + (L Page view A Read aloud Draw Highlight Erase A 24 The file UNIX file access primitives 25 Here the direction parameter for seek is set to SEEK_END to move the read-write pointer to the end of the file. Since we want to move no further, the offset is given as zero. 1seek can also be called in the same way to give the size of the file, since it , returns the new position in the file. char *getoccupier (int roonno) off_t offset; ssize_t read: /* open the file first time around / if( infile w-1 & (infile.open("residents", O_RDONLY)) = -1) return (NULL) 1. couldn't open file/ ) off_t filesize int filedes: offset - roomno - 1). NAMELENGTH: filesize - I seek(filedes, coff_t)0, SEEK_END); Exercise 2.8 Write a function that uses lseek to get the size of an open file, but appears not to have changed the value of the read-write pointer. /* find roon slot and read occupier's name / 17 (1seek infile, offset, SEEK_SET) -- -1) return (NULL); if (red - read (infile, namebut, NAMELENGTH) (-0) return (NULL) /* create a string by replacing the newline character with the null terminator / nanebuf [read - 1] - '\0'; return tranebuf): 1 screen rec 2.1.11 The hotel example As a highly contrived, but possibly illuminating, example, suppose we have a file residents for recording the names of the residents of a hotel. Line 1:ontains the name of the occupant of room 1, line 2 the name of the occupant of room 2 and so on as you can see this is a hotel with a peculiarly well-behaved room-numbering system). Each line is exactly 41 characters in length, the first 46 characters containing the occupant's name while the 41st is a newline character which makes the file displayable with the UNIX cat command. The following getoscupier function will, given an integer room number, calculate the location of the first byte of the occupier's name, then nove to that position and read the data contained there. It returns either a pointer to a string containing the occupier's name, or a null pointer on error (we will use the value for NULL as defined in for this). Note how we give the file descriptor variable infile an initial value of -1. By testing for this we can ensure the file is opened just once. Assuming that the hotel contains 10 rooms, the following program will successively call getoccupier to scan through the file, using the printf routine from the Standard I/O Library to display each name it finds: 1. listoc -- list all occupants' nanes / #define NROOMS 10 main() int: bre char .geoccupier (int), *p; for(j = 1; i include #include #define NAMELENGTH 41 char nanebuf (NAMELENGTH: int infile. -11 1+ buffer to hold name */ It will hold file descriptor Exercise 2.9 Invent a mechanism for deciding whether a room is empty. Modify getoccupier, and the data file if necessary, to reflect this. Now write a routine called findfree to locate the lowest numbered free room. Type here to search E a E 1 HT --- W H ? 19:23 2/26/21 Bb Home Page - COS X Bb Unix System Progr x PDF Chap 1-3.pdf Bb https://learn-us-e X Bb https://learn-us-ea X C Just These Functio X M Inbox (284) - fekac X + File C:/Users/fiktiru/Documents/Chap 1-3.pdf To w o + : oo 17 of 36 + (L Page view A Read aloud Draw Highlight Erase A 24 The file UNIX file access primitives 25 Here the direction parameter for seek is set to SEEK_END to move the read-write pointer to the end of the file. Since we want to move no further, the offset is given as zero. 1seek can also be called in the same way to give the size of the file, since it , returns the new position in the file. char *getoccupier (int roonno) off_t offset; ssize_t read: /* open the file first time around / if( infile w-1 & (infile.open("residents", O_RDONLY)) = -1) return (NULL) 1. couldn't open file/ ) off_t filesize int filedes: offset - roomno - 1). NAMELENGTH: filesize - I seek(filedes, coff_t)0, SEEK_END); Exercise 2.8 Write a function that uses lseek to get the size of an open file, but appears not to have changed the value of the read-write pointer. /* find roon slot and read occupier's name / 17 (1seek infile, offset, SEEK_SET) -- -1) return (NULL); if (red - read (infile, namebut, NAMELENGTH) (-0) return (NULL) /* create a string by replacing the newline character with the null terminator / nanebuf [read - 1] - '\0'; return tranebuf): 1 screen rec 2.1.11 The hotel example As a highly contrived, but possibly illuminating, example, suppose we have a file residents for recording the names of the residents of a hotel. Line 1:ontains the name of the occupant of room 1, line 2 the name of the occupant of room 2 and so on as you can see this is a hotel with a peculiarly well-behaved room-numbering system). Each line is exactly 41 characters in length, the first 46 characters containing the occupant's name while the 41st is a newline character which makes the file displayable with the UNIX cat command. The following getoscupier function will, given an integer room number, calculate the location of the first byte of the occupier's name, then nove to that position and read the data contained there. It returns either a pointer to a string containing the occupier's name, or a null pointer on error (we will use the value for NULL as defined in for this). Note how we give the file descriptor variable infile an initial value of -1. By testing for this we can ensure the file is opened just once. Assuming that the hotel contains 10 rooms, the following program will successively call getoccupier to scan through the file, using the printf routine from the Standard I/O Library to display each name it finds: 1. listoc -- list all occupants' nanes / #define NROOMS 10 main() int: bre char .geoccupier (int), *p; for(j = 1; i include #include #define NAMELENGTH 41 char nanebuf (NAMELENGTH: int infile. -11 1+ buffer to hold name */ It will hold file descriptor Exercise 2.9 Invent a mechanism for deciding whether a room is empty. Modify getoccupier, and the data file if necessary, to reflect this. Now write a routine called findfree to locate the lowest numbered free room. Type here to search E a E 1 HT --- W H ? 19:23 2/26/21
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
