A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
fseek moves the file position indicator to an arbitrary byte offset. Three reference points: SEEK_SET (beginning of file), SEEK_CUR (current position), SEEK_END (end of file). Combined with ftell, which reports the current position, you can build index tables, skip headers, jump to records, and implement random access over binary data. This is how databases, file format parsers, and media players navigate files without reading them sequentially.
#include <stdio.h>int main(void) {FILE *fp = fopen("notes.txt", "r");if (fp == NULL) {perror("fopen");return 1;}fseek(fp, 3, SEEK_SET);int ch = fgetc(fp);printf("Byte at offset 3: '%c' (ASCII %d)\n", ch, ch);fseek(fp, -1, SEEK_END);ch = fgetc(fp);printf("Last byte: '%c' (ASCII %d)\n", ch, ch);fseek(fp, 0, SEEK_SET);fseek(fp, 5, SEEK_CUR);ch = fgetc(fp);printf("Byte at offset 5: '%c' (ASCII %d)\n", ch, ch);fclose(fp);return 0;}
A common idiom: seek to the end, ask for the position (which equals the file size), then seek back to the start. This gives you the file length without reading the entire contents.
#include <stdio.h>int main(void) {FILE *fp = fopen("notes.txt", "rb");if (fp == NULL) {perror("fopen");return 1;}fseek(fp, 0, SEEK_END);long file_size = ftell(fp);rewind(fp);printf("File size: %ld bytes\n", file_size);fclose(fp);return 0;}
ftell returns long, which is 32 bits on some platforms. Files larger than 2 GB overflow a long. For large files, use fseeko/ftello with off_t (POSIX) or _fseeki64/_ftelli64 (Windows). In new code targeting 64-bit systems, this is less of a concern, but embedded and legacy 32-bit targets still hit this wall.
fseek and ftell together give you random access. You can build file indexes, skip fixed-size headers, and jump directly to the record you need without scanning everything before it. This is the foundation of binary file format parsing. Next: binary file access, where we read and write raw structs and byte arrays.
Finish the lesson once you have worked through the material. This awards ★ 30 XP.