aboutsummaryrefslogtreecommitdiff
path: root/ch8/8-04_fseek.c
diff options
context:
space:
mode:
authorzlg <zlg@zlg.space>2022-01-09 20:18:52 -0800
committerzlg <zlg@zlg.space>2022-01-09 20:18:52 -0800
commitbfd18a52e1baa4da7040b8df04a3f29e83b0d6b6 (patch)
tree37cd17530ef47fff106c0a30fb26fe70cca429d8 /ch8/8-04_fseek.c
parentSolve exercise 8-3: fflush and fclose (diff)
downloadknr-bfd18a52e1baa4da7040b8df04a3f29e83b0d6b6.tar.gz
knr-bfd18a52e1baa4da7040b8df04a3f29e83b0d6b6.tar.bz2
knr-bfd18a52e1baa4da7040b8df04a3f29e83b0d6b6.tar.xz
knr-bfd18a52e1baa4da7040b8df04a3f29e83b0d6b6.zip
Solve Exercise 8-4: fseek() implementation
Diffstat (limited to '')
-rw-r--r--ch8/8-04_fseek.c34
1 files changed, 34 insertions, 0 deletions
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 <errno.h>
+#include <string.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+#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! */