From bfd18a52e1baa4da7040b8df04a3f29e83b0d6b6 Mon Sep 17 00:00:00 2001 From: zlg Date: Sun, 9 Jan 2022 20:18:52 -0800 Subject: Solve Exercise 8-4: fseek() implementation --- ch8/8-04_fseek.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 ch8/8-04_fseek.c (limited to 'ch8/8-04_fseek.c') diff --git a/ch8/8-04_fseek.c b/ch8/8-04_fseek.c new file mode 100644 index 0000000..b968ca2 --- /dev/null +++ b/ch8/8-04_fseek.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include "syscalls.h" +/* The C Programming Language: 2nd Edition + * + * Exercise 8-4: The standard library function + * + * int fseek(FILE *fp, long offset, int origin) + * + * is identical to `lseek` except that `fp` is a file pointer instead of a file + * descriptor and the return value is an `int` status, not a position. Write + * `fseek`. Make sure that your `fseek` coordinates properly with the buffering + * done for the other functions of the library. + * + * Notes: Page 174 covers lseek, where it describes fseek's return value as 0 + * for success and non-zero for errors. + */ + +int main() { + FILE *fp = fopen("8-04_fseek.c", "r"); + if (fp == NULL) { + exit(1); + } + fseek(fp, -34L, 2); + puts(fp->base); + fflush(stdout); + fclose(fp); + return 0; +} + +/* If you see this, 8-4 works! */ -- cgit v1.2.3-54-g00ecf