File Handling
Write and read a temporary runtime file while understanding Circuit’s filesystem boundary.
Complete program
#include <stdio.h>
int main(void) {
FILE *file = fopen("note.txt", "w");
if (!file) { perror("open for writing"); return 1; }
if (fputs("Hello from a file\n", file) == EOF) { fclose(file); return 1; }
if (fclose(file) != 0) return 1;
file = fopen("note.txt", "r");
if (!file) { perror("open for reading"); return 1; }
char line[64];
if (fgets(line, sizeof line, file)) printf("Read: %s", line);
int failed = ferror(file);
if (fclose(file) != 0) failed = 1;
return failed ? 1 : 0;
return 0;
}
Opens a new program without replacing your saved code. Press Run when you are ready.
How it works
Opening with w creates or truncates the runtime file. Closing it finishes that write before the second open reads from the beginning. Each operation can fail, so the example checks the important return values.
Circuit provides an in-memory WASI directory for each execution. This file does not appear on your computer, enter your saved program library, or survive another run. For persistent files and full operating-system APIs, use a local toolchain.
Expected output
Read: Hello from a fileConcepts to practice
- FILE streams
- fopen / fclose
- Error checking
- Ephemeral filesystem
Predict a different input or change one value, then compare the result with your prediction. Keep changes within the documented bounds and read warnings before trusting an answer.