Avatar ·

How to get a list of files in a directory in C++? (cross-platform)

Hello everyone! I'm a beginner C++ developer, and I have a problem. I need to write a function that gets a list of all files in a given directory. I work on Windows, but I would like the code to work on Linux (or macOS) without rewriting.\n\nThe problem is that I found several different ways. For example, on Windows there are WinAPI functions `FindFirstFile` / `FindNextFile`. On Linux — `opendir` / `readdir`. But this is terribly inconvenient: writing `#ifdef _WIN32` and duplicating code.\n\nI heard that in C++17 something like `std::filesystem` appeared. But I'm not sure if it works in practice. My compiler (MSVC 2022) seems to support C++17, but I'm afraid that on old servers or embedded systems it might not compile.\n\nHere's an example of what I'm trying to do (pseudocode):\n\n```