File Notes
Use the filesystem standard library to write a file, check its existence, and read it back.
Core concepts
import std.fs, fs.write, fs.exists, fs.read
How the learner should approach this project
Do not paste the complete program immediately. Create the file, type the first step, run vpp check, and then run the program when a complete entry point exists. Read the explanation before looking at the snippet. After each step, predict what the new code should do. This turns the page into a lesson rather than a code dump.
Step 1: Import std.fs
Standard library modules are imported before program definitions. std.fs exposes the filesystem operations used in this project.
After typing the snippet, identify the new names introduced by the step. Ask what each name represents, what type the compiler should infer or check, and what value should exist after the code runs.
import std.fsStep 2: Write, check, and read
The program creates a path and a text body. fs.write stores the text. fs.exists verifies that the path exists. fs.read loads the content back into the program.
The sequence demonstrates a common application pattern: prepare data, perform an external operation, verify or inspect the result, then continue.
Before moving forward, explain how this step connects to the previous one. In particular, identify which values cross a function boundary, which values change, and which values are guaranteed by the type system.
import std.fs
fn main() -> int {
let path = "note.txt"
let body = "v++ file notes
Built by Shaurya
Open source on GitHub"
fs.write(path, body)
print(fs.exists(path))
let content = fs.read(path)
print(content)
return 0
}Complete source
import std.fs
fn main() -> int {
let path = "note.txt"
let body = "v++ file notes
Built by Shaurya
Open source on GitHub"
fs.write(path, body)
print(fs.exists(path))
let content = fs.read(path)
print(content)
return 0
}Expected behavior
The complete program should produce the following output when run with the command shown below. Exact formatting should follow the current V++ runtime.
true v++ file notes Built by Shaurya Open source on GitHub
Run the project
vpp run projects/19-file-notes/main.vppWhat the learner should understand after this project
Filesystem operations cross the boundary between pure program logic and the outside world. The code must treat paths and file contents as external data.
Common mistakes to teach
- Reading a file before writing it
- Using the wrong path
- Ignoring the difference between a path and the file contents
Practice extension
Write a second file, read it, and print both contents. Then change the path to a nested path if your environment permits it.
A strong learner should be able to explain the program without looking at the code, rebuild the core idea from memory, and make the practice change without copying a solution.
Full program
import std.fs
fn main() -> int {
let path = "note.txt"
let body = "v++ file notes
Built by Shaurya
Open source on GitHub"
fs.write(path, body)
print(fs.exists(path))
let content = fs.read(path)
print(content)
return 0
}