File Browser
Recall that fopen returns a pointer to a FILE, which happens to be a struct. Within that struct are a number of fields that keep track of an opened file’s state. The actual implementation is a bit complex! But consider the simplified definition of FILE below.
typedef struct
{
bool read_only; // True if file was opened only for reading (as with "r"), not writing
char *first_byte; // Pointer to the first byte of the file
char *current_byte; // Pointer to the current position in the file
int size; // Size of the file in bytes
}
FILE;
Assume that first_byte and current_byte point to memory that represents an opened file, in which case writing to those addresses, as via fwrite, will have the effect of updating the file on disk. And recall that files are read, as via fread, by default, from their first byte through their last. And, so, when a file is first opened, both first_byte and current_byte will point to that file’s first byte. And each time a byte is read thereafter, current_byte will be incremented.
-
(2 points.) It turns out there’s a function called
fgetposthat can tell you how many bytes awaycurrent_byteis fromfirst_byte. For instance, if a file has been opened but no bytes have been read,fgetposwould return0; if a file has been opened and one byte has been read,fgetposwould return1. Complete the (simplified) implementation offgetposbelow, assuming the definition ofFILEabove.int fgetpos(FILE *stream) { // TODO }
-
(4 points.) It turns out there’s also a function called
fsetposthat allows you to “seek” (i.e., fast-forward or rewind) to any byte in a file, relative to its first byte. For instance, a call tofsetpos(file, 0), wherefileis pointer to aFILE, would have the effect of (re)settingcurrent_bytetofirst_byte. Complete the (simplified) implementation offsetposbelow, assuming again the definition ofFILEabove. Your implementation should returnfalsein cases of error, which we leave to you to identify; otherwise, it should returntrue.bool fsetpos(FILE *stream, int offset) { // TODO }
-
(6 points.) Recall that
fwriteis a function that writes data to a file if and only if that file has been opened for writing, as via"w". It turns out it also updates aFILEin the following ways:- The data stored in
bufferis written to that file, starting atcurrent_byteand continuing forsizebytes. - When writing is complete,
current_byteis updated to the byte immediately after the last byte written. - The
sizeof the file, as stored in thestruct, is adjusted as needed.
Complete the (simplified) implementation of
fwritebelow, assuming again the definition ofFILEabove. Your implementation should returnfalsein cases of error, which we leave to you to identify; otherwise, it should returntrue.typedef unsigned char BYTE; bool fwrite(BYTE *buffer, int size, FILE *stream) { // TODO } - The data stored in
-
(2 points.) Recall that
feofis a function that indicates whether a caller has read past the end of a file. Complete the (simplified) implementation offeofbelow, assuming again the definition ofFILEabove. Your implementation should returntrueif a caller has read past the end of a file; otherwise it should returnfalse.bool feof(FILE *stream) { // TODO }