Library contents

C++ std::filesystem::exists

Use a filesystem status query to decide whether a receipt is available for a reprint preview.

A simple example

std::filesystem::exists

  1. Start with No file named a.txt
  2. Do this Open a.txt for reading
  3. Result File missing
The missing-path outcome lets the caller offer an empty starting state or ask for another name.

Syntax

C++ syntax
std::filesystem::exists(receipt)
Run these examples in C++

Save one example at a time as lesson.cpp. Use C++17 on macOS or Linux with a POSIX shell.

c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lesson

Open the first receipt of the day

A new checkout starts with an empty receipt folder. When the cashier asks to reprint today's first receipt, the app should explain that a receipt has yet to be saved.

From the programchar folder[] = "/tmp/checkout-XXXXXX"
ResultNo receipt yet
lesson.cpp
#include <iostream>
#include <filesystem>
#include <stdlib.h>
#include <unistd.h>

int main() {
    char folder[] = "/tmp/checkout-XXXXXX";
    if (!mkdtemp(folder)) return 1;
    std::filesystem::path desk(folder);
    bool available = std::filesystem::exists(desk / "today.txt");
    std::filesystem::remove(desk);
    if (!available) std::cout << "No receipt yet\n";
    return 0;
}
Output
No receipt yet

The program owns a fresh, empty directory, so in this run the named receipt is known to be absent.

Advertisements

Read a saved shift record

The closing clerk saved the file before leaving. Open the expected file and show the desk whether it is available.

Givenrecord.txt exists
Worked resultFile available

Handle an unsubmitted handover

The next shift opens an empty inbox before the first clerk submits a file. Open the expected file and show the desk whether it is available.

Givenrecord.txt missing
Worked resultAsk for the file

What to watch for

A status snapshot can become stale. Handle failure again when the real application opens the receipt. This example uses POSIX mkdtemp on macOS or Linux.

Documentation for std::filesystem::exists