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
fgetpos
that can tell you how many bytes awaycurrent_byte
is fromfirst_byte
. For instance, if a file has been opened but no bytes have been read,fgetpos
would return0
; if a file has been opened and one byte has been read,fgetpos
would return1
. Complete the (simplified) implementation offgetpos
below, assuming the definition ofFILE
above.int fgetpos(FILE *stream) { // TODO }
-
(4 points.) It turns out thereās also a function called
fsetpos
that 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)
, wherefile
is pointer to aFILE
, would have the effect of (re)settingcurrent_byte
tofirst_byte
. Complete the (simplified) implementation offsetpos
below, assuming again the definition ofFILE
above. Your implementation should returnfalse
in 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
fwrite
is 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 aFILE
in the following ways:- The data stored in
buffer
is written to that file, starting atcurrent_byte
and continuing forsize
bytes. - When writing is complete,
current_byte
is updated to the byte immediately after the last byte written. - The
size
of the file, as stored in thestruct
, is adjusted as needed.
Complete the (simplified) implementation of
fwrite
below, assuming again the definition ofFILE
above. Your implementation should returnfalse
in 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
feof
is a function that indicates whether a caller has read past the end of a file. Complete the (simplified) implementation offeof
below, assuming again the definition ofFILE
above. Your implementation should returntrue
if a caller has read past the end of a file; otherwise it should returnfalse
.bool feof(FILE *stream) { // TODO }