The last lesson showed the raw descriptors underneath everything. In day-to-day C you rarely call read and write directly for files — you use the standard I/O layer: FILE *, fopen, fgets, fread, and friends. It buffers for you and comes with convenient helpers for both text and binary data. This is the toolkit you will reach for in nearly every challenge in this section, so it is worth having in one place.
You open a file with fopen, which takes a path and a mode string and returns a FILE * — the buffered wrapper around a descriptor from the last lesson. It returns NULL if the file could not be opened, so you always check:
1FILE *fp = fopen("data.txt", "r");
2if (fp == NULL)
3{The common modes are "r" to read, "w" to write (truncating any existing contents), and "a" to append. Add a b — "rb", "wb" — when the data is binary rather than text. When you are done, fclose(fp) flushes anything still buffered and releases the descriptor.
Most input is text, read a line at a time. fgets reads one line into a buffer you provide, and crucially it takes the buffer's size, so it will never overflow it:
1char line[128];
2while (fgets(line, sizeof(line), fp) != NULL)The loop keeps going until fgets returns NULL, which happens at end of file. Here it is end to end — writing three lines, then reading them back:
1#include <stdio.h>
2
3int main(void)
4{
5gcc lineio.c -o lineio./lineioread: alpharead: bravoread: charlieWhen you do not want to pick a buffer size at all, getline allocates and grows one for you — which is why the challenges in this section reach for it constantly. You hand it the address of a char * (starting as NULL) and a size_t; it reads a full line however long, updating both, and returns the number of bytes read, or -1 at end of file. The buffer it allocates is yours, so free it when the loop ends:
1#include <stdio.h>
2#include <stdlib.h>
3
4int main(void)One catch trips up almost everyone: getline is a POSIX function, not part of ISO C (the same is true of the ssize_t type its result is often stored in). With GCC's defaults it is available, but compile in a strict standards mode like -std=c11 and it disappears — the compiler reports an implicit declaration. You expose it by setting the feature-test macro _POSIX_C_SOURCE to 200809L (POSIX-2008, the revision that standardized getline):
gcc -std=c11 getline.c -o getlinegetline.c:14:12: error: implicit declaration of function ‘getline’ [-Wimplicit-function-declaration]gcc -std=c11 -D_POSIX_C_SOURCE=200809L getline.c -o getline./getlineread: alpharead: bravoSo keep -D_POSIX_C_SOURCE=200809L within reach — or put #define _POSIX_C_SOURCE 200809L as the very first line of the file, before any #include. It is covered again, in context, in the ASCII challenges introduction.
When a file holds raw bytes rather than text — the serialized challenges — you use fread and fwrite. fread takes a destination, the size of one element, how many elements to read, and the file, and returns how many it actually read. Opening in binary mode ("rb"/"wb") keeps the system from translating bytes on you:
1#include <stdint.h>
2#include <stdio.h>
3
4int main(void)gcc binio.c -o binio./biniogot 100got 200got 300The loop condition — fread(...) == 1 — is the binary equivalent of checking for NULL: it keeps reading one element at a time until fread comes up short at the end of the file.
Reading gets you a line of text; the next job is usually pulling values out of it. You already have the tools from the strings lesson: strtok to split a line into tokens on a delimiter, strtol (or atoi) to turn a token into a number, and sscanf to pull fields straight out of a formatted line:
1int day, month, year;
2sscanf(line, "%d/%d/%d", &day, &month, &year);Which one you use depends on how regular the input is — sscanf when the format is fixed, strtok when you are walking a variable list of fields.
Two habits round this out. Always fclose what you fopen: it flushes the buffer (so your writes actually reach disk) and frees the descriptor. And know how each reader signals end of file — fgets returns NULL, getline returns -1, and fread returns fewer elements than you asked for. Those return values are the EOF check, which is why every read loop in this section is written around them rather than testing feof up front.
That is the whole input pipeline: open a file, read it line by line or record by record, parse what you read into arrays and structs, and close up — everything the challenges rely on. Next, the Tools section covers building and debugging programs larger than a single file, starting with Compilation.