Is There a Os Library for C++

Let's say I want to make a file manager, And I need to fetch the names of all the files in a given path. In python I would do something like: os.listdir(path). So is a c++ library like the OS module.

2

2 Answers

You can use

#include <filesystem>

for this.

For some older compilers you might need to check if only experimental filesystem is available and use that.

#pragma once

#if __has_include(<filesystem>)

#include <filesystem>
namespace filesystem = std::filesystem;

#else

#include <experimental/filesystem>
namespace filesystem = std::experimental::filesystem;

#endif

Then to iterate over files:

   for (auto it : filesystem::directory_iterator("path/to/iterate"))
   {
       // Use it.path
   }

   // Or recursively
   for (auto it : filesystem::recursive_directory_iterator("path/to/iterate"))
   {
       // Use it.path
   }

3

You can import boost's filesystem libs like so:

#include <boost/filesystem.hpp>

Perhaps that works?

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest