1,105,417 Community Members

read all files in a directory

Member Avatar
jasssvj
Newbie Poster
4 posts since Apr 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi!

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

Sincerely
Jasmina Jeleva!

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

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

Member Avatar
vijayan121
Posting Virtuoso
1,769 posts since Dec 2006
Reputation Points: 1,097 [?]
Q&As Helped to Solve: 329 [?]
Skill Endorsements: 16 [?]
 
1
 

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)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article