Hi!

How can I read all files in a directory, when I don't know which files are
there?

Sincerely
Jasmina Jeleva!

Depends totally on the compiler and the operating system. There is no standard way to do it.

Hi!

How can I read all files in a directory, when I don't know which files are
there?

Sincerely
Jasmina Jeleva!

you could use the boost filesystem library.
it is portable across unix, linux and windows.

here is a sample program which prints the names of all files in a directory.

#include <iostream>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
using namespace boost::filesystem; 
using namespace std;

void show_files( const path & directory, bool recurse_into_subdirs = true )
{
  if( exists( directory ) )
  {
    directory_iterator end ;
    for( directory_iterator iter(directory) ; iter != end ; ++iter )
      if ( is_directory( *iter ) )
      {
        cout << iter->native_directory_string() << " (directory)\n" ;
        if( recurse_into_subdirs ) show_files(*iter) ;
      }
      else 
        cout << iter->native_file_string() << " (file)\n" ;
  }
}

int main()
{
    show_files( "/usr/share/doc/bind9" ) ;
}

on my machine it produced the following output.

g++ -std=c++98 -Wall -I/usr/local/include file_system.cpp /usr/local/lib/libboost_filesystem.a ; ./a.out

/usr/share/doc/bind9/arm (directory)
/usr/share/doc/bind9/arm/Bv9ARM.ch01.html (file)
/usr/share/doc/bind9/arm/Bv9ARM.ch02.html (file)
/usr/share/doc/bind9/arm/Bv9ARM.ch03.html (file)
/usr/share/doc/bind9/arm/Bv9ARM.ch04.html (file)
/usr/share/doc/bind9/arm/Bv9ARM.ch05.html (file)
/usr/share/doc/bind9/arm/Bv9ARM.ch06.html (file)
/usr/share/doc/bind9/arm/Bv9ARM.ch07.html (file)
/usr/share/doc/bind9/arm/Bv9ARM.ch08.html (file)
/usr/share/doc/bind9/arm/Bv9ARM.ch09.html (file)
/usr/share/doc/bind9/arm/Bv9ARM.html (file)
/usr/share/doc/bind9/misc (directory)
/usr/share/doc/bind9/misc/dnssec (file)
/usr/share/doc/bind9/misc/format-options.pl (file)
/usr/share/doc/bind9/misc/ipv6 (file)
/usr/share/doc/bind9/misc/migration (file)
/usr/share/doc/bind9/misc/migration-4to9 (file)
/usr/share/doc/bind9/misc/options (file)
/usr/share/doc/bind9/misc/rfc-compliance (file)
/usr/share/doc/bind9/misc/roadmap (file)
/usr/share/doc/bind9/misc/sdb (file)
/usr/share/doc/bind9/CHANGES (file)
/usr/share/doc/bind9/COPYRIGHT (file)
/usr/share/doc/bind9/FAQ (file)
/usr/share/doc/bind9/README (file)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.