Before using the OS APIs directly, take a look if the STL or boost already provides this functionality. Also be aware for a cross-platform software you have to implement it for all of them separately.
Example (File system access)
#include <sys/types.h> #include <dirent.h> void listFiles(const std::string& folder, std::vector<std::string>& files) { DIR* posDir = opendir(folder.c_str()); struct dirent* data; while ((data = readdir(posDir)) != NULL) { files.push_back(data->d_name); } closedir(posDir); }
#include <windows.h> void listFiles(const std::string& folder, std::vector<std::string>& files) { std::string regEx(folder + "\\*"); WIN32_FIND_DATA data; HANDLE hdl; if ((hdl = FindFirstFile(regEx.c_str(), &data)) != INVALID_HANDLE_VALUE) { do { files.push_back(data.cFileName); } while (FindNextFile(hdl, &data) != 0); FindClose(hdl); } }
That scenario for example is already implemented in boost for a long time and got into the STL since C++17.