C++ Filesystem
The writing-files page left pine inside note.txt from box.cpp and kept the screen empty on purpose. This page stays in the Errors and Files module on purpose. The workshop file on disk stays box.cpp so you keep one familiar build path. The new work asks whether that path exists with std::filesystem::exists("note.txt"). When the answer is true, the program prints pine. Inheritance waits for the next page.
A filesystem helper answers questions about paths without opening a stream for every check. On this page you write #include <filesystem> and then test std::filesystem::exists("note.txt"). Reading exists is reading a yes-or-no answer about that disk name. Reading the if body is reading the print of pine when the file is already there from the writing page. Keep #include <iostream> so std::cout can show pine.
Programs use filesystem checks when you want to know a path is present before you read or write again. You do not keep the quiet ofstream body as the lesson body on this page. Extra inheritance tools wait for the next page on purpose. Here the point is only asking whether note.txt exists and printing pine when it does. The build command stays familiar: one source file box.cpp, one output name box, one run that prints pine when the file is present. You compile only box.cpp again for this workshop file on disk. Use -std=c++17 so <filesystem> resolves.
Think of exists as asking the sticky note shelf if the note is still there. The previous page filled the note. This page only asks and then prints pine. Do not expect an empty screen on a successful run with note.txt present. The printed pine is part of the lesson.
Inside main, write if (std::filesystem::exists("note.txt")) { std::cout << "pine" << std::endl; }, then return 0;. Keep #include <filesystem> and #include <iostream> at the top of the file. Do not write with ofstream in the checked body. Do not start an inheritance hierarchy on this page.
Walk the run carefully from the first line of main. The program starts at main. exists asks about note.txt. When the path is present, the if body prints pine. Then main returns and the program ends. There is one printed line on the screen: pine. The checked file always asks and prints that way when the file is present.
Say the steps out loud once while you read the file. Ask whether note.txt exists. If yes, print pine. Return. That short story is the whole workshop path. Nothing else belongs in the checked body. No ofstream. No getline. No base class yet. When the screen shows pine and note.txt is still on disk, the story matched the source.
If you forget #include <filesystem>, the build fails about std::filesystem. The checked file keeps that include.
If note.txt is missing, the if body does not run and the screen stays empty. Re-run the writing page or create note.txt with pine first, then run this page again. The checked file does not invent a missing-file throw on this page.
A common mistake is to keep the ofstream write and never call exists. Another common mistake is to expect pine without a present note.txt. A third mistake is to invent a full directory walk API on this page. A fourth mistake is to wait for cin. A fifth mistake is to start inheritance classes here.
This page stops before the next tool, where one class builds on another. The next page names that tool once at the end. The check you have just written only shows asking whether note.txt exists and printing pine. Keep the path name note.txt so the order stays clear for a first careful reading of the file.
Remember that exists does not print by itself. You still write the cout line inside the if. After a successful run with the file present the screen shows pine. After you undo a bad experiment the checked file returns to that exists-and-print path. Stability of the checked file matters as much as learning filesystem itself.
Open box.cpp. Remove the ofstream write lines from the writing demo. Keep #include <iostream>. Add #include <filesystem>. In main, ask exists("note.txt"), print pine when true, and return. The program file name on disk stays box.cpp. The checked file is below, and the new lines are the filesystem include and the exists check.
#include <iostream>
#include <filesystem>
int main()
{
if (std::filesystem::exists("note.txt")) {
std::cout << "pine" << std::endl;
}
return 0;
}
Rebuild with g++ -std=c++17 box.cpp -o box, then run ./box from the folder where note.txt lives. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 box.cpp -o box. The screen shows pine when the file is present.
Check the printed pine first. Then confirm note.txt is still in the folder. The table gives each row its piece, so compare your run with it line by line before you move on to experiments. If the screen is empty, note.txt is missing or the if body never ran. If the build fails about filesystem, check #include <filesystem> and -std=c++17. If you still see an ofstream-only quiet run, the old writing body is still there.
📊 filesystem exists and what the screen shows
| Piece | What the screen shows |
|---|---|
| First | pine, when note.txt exists from the writing page |
| Last | pine, because the if body prints once and returns |
| Rule | filesystem::exists asks about a disk name; cout prints pine when true |
| Screen | pine |
Try one change at a time. Temporarily rename note.txt aside so exists is false, rebuild, and see an empty screen. Restore note.txt. Next, temporarily print oak instead of pine inside the if, rebuild, see oak, then restore pine. Each experiment changes one thing on purpose, so you see either a missing-path quiet run or a different print. Undo each change before the next one starts, so the file always returns to the checked workshop version with exists and pine once again after every single careful try back on disk again.
This page ends the filesystem tool and stops before the next one. The next page is inheritance, which starts a class that builds on another. The check you have just written only shows asking whether note.txt exists from box.cpp and printing pine. You leave with pine on the screen when the path is present. Count the printed lines before you leave. There is exactly one printed line on that final screen when note.txt is there, and that line is pine alone right there now after the final successful build run on disk today now after the filesystem exists check.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top