Reference

Programming languages

Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.

Scope and conventions

TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.

Files & paths

Join paths using the platform’s path API

C++

filesystem::path models native paths and supports composition through /.

Fragment

Include <filesystem>. Lexical normalization is distinct from resolving filesystem links or enforcing access.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <filesystem>

auto file = std::filesystem::path(directory) / "settings.json";

List the immediate entries in a directory

C++

directory_iterator visits immediate entries. Filesystem errors can throw; overloads taking error_code allow explicit error handling.

Fragment

Include <filesystem> and <iostream>. Traversal order is not guaranteed.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <filesystem>
#include <iostream>

for (const auto& entry : std::filesystem::directory_iterator(directory)) {
    std::cout << entry.path() << '\n';
}

Create a directory and missing parents

C++

create_directories creates missing parents and returns whether it created a directory. false does not necessarily mean failure: it can already exist.

Fragment

Include <filesystem>; this overload reports errors through filesystem_error exceptions.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <filesystem>

std::filesystem::create_directories(directory);

Copy a chosen file with an overwrite policy

C++

copy_file with no overwrite option fails on an existing destination, giving the caller an explicit conflict to handle.

Fragment

Include <filesystem>. Use copy_options::overwrite_existing only when replacement is intended.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <filesystem>

std::filesystem::copy_file(source, destination);

Read file text with explicit error and encoding behavior

C++

A file stream reports failures through state bits by default. Binary mode avoids platform text translation; reading bytes does not establish an encoding.

Fragment

Usage statements belong in a function. The stream closes through RAII.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <fstream>
#include <iterator>
#include <stdexcept>
#include <string>

std::ifstream input("notes.txt", std::ios::binary);
if (!input) throw std::runtime_error("open failed");
std::string text((std::istreambuf_iterator<char>(input)), {});
if (input.bad()) throw std::runtime_error("read failed");

Write or replace file contents

C++

Check stream state after opening and writing; explicitly close and check when close-time failures matter.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <fstream>
#include <stdexcept>

std::ofstream output("output.txt", std::ios::binary);
output << "ready\n";
output.close();
if (!output) throw std::runtime_error("write/close failed");