finished most of it I just need a function that finds an empty room and if there
ID: 640184 • Letter: F
Question
finished most of it I just need a function that finds an empty room and if there are none it returns -1 otherwise it adds the guest into the first empty room. (Please show Output)
#include <stidio.h>
#include <fcnt1.h>
#include <unistd.h>
#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)='';
return (namebuf);
Explanation / Answer
#include #include #include #define NAMELENGTH 41 // char *getoccupier(int roomno); char namebuf[NAMELENGTH]; // buffer to hold name int infile = -1; // will hold fild 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)) { printf("error: can not open residents "); return(NULL); } offset = (roomno - 1)*NAMELENGTH; // find room slot and read occupier's name if(lseek(infile,offset,SEEK_SET) == -1) { close(infile); return(NULL); } if((nread = read(infile,namebuf,NAMELENGTH))Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.