Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard way to do findfirst, findnext with gcc on linux using stl?

Tags:

c++

file

linux

gcc

stl

I can't seem to find the _findfirst / findfirst, _findnext / findnext API on gcc for Linux, and would actually rather use the Standard Template Library (STL) for that if it is included there.

Does anyone know what API there is available for listing files in a directory under Linux for C++ (gcc)?

like image 975
TheSeeker Avatar asked Oct 24 '08 22:10

TheSeeker


4 Answers

Since C++17 the standard library contains std::filesystem which has its source in Boost.Filesystem. Nowadays std::filesystem::directory_iterator is the obvious choice since it is platform-independent, offers better abstraction than _findfirst/findnext/opendir/readdir/closedir and doesn't introduce any dependencies. If you can't use a C++17-compliant compiler use Boost for now and switch over later.

like image 67
Martin Konrad Avatar answered Oct 03 '22 04:10

Martin Konrad


It's not a C++-style API, but the API you aren't finding (the Linux/Unix correspondent of DOS/Windows-style findfirst/findnext) is opendir/readdir/closedir.

The main advantage of using opendir/readdir/closedir is that you do not need any extra library (it's part of the C library, which you are already using). In fact, the Boost filesystem library uses opendir/readdir/closedir to get the list of files in a directory.

References:

  • http://www.opengroup.org/onlinepubs/009695399/functions/opendir.html
  • http://www.opengroup.org/onlinepubs/009695399/functions/readdir.html
  • http://www.opengroup.org/onlinepubs/009695399/functions/closedir.html
like image 31
CesarB Avatar answered Oct 03 '22 03:10

CesarB


Check out the Boost.Filesystem library.

In particular, the basic_directory_iterator.

like image 27
Jeffrey Martinez Avatar answered Oct 03 '22 04:10

Jeffrey Martinez


The STL does not, yet, have functions for listing files in a directory. But it does have functions for opening files you are already aware of.

Aside from Boost.Filesystem, there is also STLSoft

like image 44
Max Lybbert Avatar answered Oct 03 '22 05:10

Max Lybbert