C++ std::filesystem::is_regular_file
Distinguish regular files from directories.
A simple example
std::filesystem::is_regular_file
- Start with A regular file a.txt
- Do this Test whether it is a directory
- Result No
Syntax
fs::is_regular_file(upload)
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
./lessonTell an uploaded file apart from its folder
A registration desk receives documents through an upload folder. The clerk needs to confirm that the submitted entry is a regular file and that the enclosing location is a directory before processing the batch.
#include <unistd.h>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <stdexcept>
namespace fs = std::filesystem;
int main() {
char pattern[] = "/tmp/file-lesson-XXXXXX";
if (!mkdtemp(pattern)) throw std::runtime_error("Cannot create workspace");
fs::path base(pattern);
try {
auto upload = base / "upload";
std::ofstream(upload) << "application";
if (fs::is_regular_file(upload) && fs::is_directory(base))
std::cout << "file and folder\n";
} catch (...) {
fs::remove_all(base);
throw;
}
fs::remove_all(base);
}
file and folder
The upload and its enclosing workspace have different entry types.
Locate a rehearsal score
The choir stores each score inside a season folder. A clerk creates an ordinary file at the given location. Which entry type should the status check report?
Locate a workshop handout
An instructor stores the current template in a class folder. A clerk creates an ordinary file at the given location. Which entry type should the status check report?
What to watch for
Status checks describe the instant of inspection. Handle failure at the subsequent open.