File Notes

Use the filesystem standard library to write a file, check its existence, and read it back.

1

Core concepts

import std.fs, fs.write, fs.exists, fs.read

2

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.

3

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.

main.vpp
import std.fs
4

Step 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.

main.vpp
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
}
5

Complete source

main.vpp
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
}
6

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
7

Run the project

terminal
vpp run projects/19-file-notes/main.vpp
8

What 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.

9

Common mistakes to teach

  1. Reading a file before writing it
  2. Using the wrong path
  3. Ignoring the difference between a path and the file contents
10

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.

11

Full program

main.vpp
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
}
vpp
$ ready. Click Test program.