Question: Please only answer the question if you know it, spamming will get a downvote and a correct answer will get an upvote. This is one
Please only answer the question if you know it, spamming will get a downvote and a correct answer will get an upvote. This is one complete question.
"Systems Programming"
Write the code of function rio_get_remaining(rio_t * rp) which returns the number of unread bytes in the rio buffer.
Write the code of function rio_get_read(rio_t * rp) which returns the number of already read bytes in the rio buffer. Hint: the difference (larger-smaller)) of two char* pointers is the number of bytes between them.
Use these two functions to write the code of function rio_seek(rio_t * rp, int offset) which changes rio_bufptr so that the next call to rio_read() will read characters in the rio buffer starting with the byte at offset from the beginning of the buffer. Function rio_seek should also adjust rio_cnt. Rio_seek should return 0 if successful and -1 if there is an error, such as offset being outside the proper range. Hint: rio_seek should call rio_get_read and rio_get_remaining to calculate the total number of bytes in the rio buffer. Hint: rio_seek should adjust rio_bufptr and rio_cnt based on offset.
Write the code of function test(rio_t * rp) that uses rio_read to read the first byte (and fill the rio buffer) and then uses functions rio_seek and rio_read to extract every tenth byte of the file associated with rp, and store these bytes in a local char array. Function test() should only use rio functions.
You can reference below:


For Reference O_APPEND enables append mode: all write operations write the data at the end of the file, extending it, regardless of the current file position. rio_cnt Buffer already read unread rio buf rio_bufptr #define RIO_BUFSIZE 8192 typedef struct { int rio_fd; int rio cnt; char *rio_bufptr; char rio_buf[RIO_BUFSIZE]; } rio_t; void rio_readinitb (rio_t *rp, int fd) { rp->rio_fd = fd; rp->rio_cnt = 0; rp->rio_bufptr = rp->rio_buf; } ssize_t rio_read (rio_t * rp, char * usrbuf, size_t n) { int cnt; if (rp->rio_cnt == 0) { /* refill if buf is empty */ rp->rio_cnt = read (rp->rio_fd, rp->rio_buf,sizeof (rp->rio_buf)); if (rp->rio_cnt rio_cnt 0) return 0; /* EOF */ else rp->rio_bufptr = rp->rio_buf; /* reset buffer ptr */ } == /* Copy min(n, rp->rio_cnt) bytes from internal buf to user buf */ cnt = n; if (rp->rio_cnt rio_cnt; memcpy(usrbuf, rp->rio_bufptr, cnt); rp->rio_bufptr += cnt; rp->rio_cnt -= cnt; return cnt; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
