read all files in a directory

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 4
Reputation: jasssvj is an unknown quantity at this point 
Solved Threads: 0
jasssvj jasssvj is offline Offline
Newbie Poster

read all files in a directory

 
0
  #1
Apr 11th, 2007
Hi!

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

Sincerely
Jasmina Jeleva!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: read all files in a directory

 
0
  #2
Apr 11th, 2007
Depends totally on the compiler and the operating system. There is no standard way to do it.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: read all files in a directory

 
0
  #3
Apr 11th, 2007
Originally Posted by jasssvj View Post
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.
  1. #include <iostream>
  2. #include <boost/filesystem/operations.hpp>
  3. #include <boost/filesystem/fstream.hpp>
  4. using namespace boost::filesystem;
  5. using namespace std;
  6.  
  7. void show_files( const path & directory, bool recurse_into_subdirs = true )
  8. {
  9. if( exists( directory ) )
  10. {
  11. directory_iterator end ;
  12. for( directory_iterator iter(directory) ; iter != end ; ++iter )
  13. if ( is_directory( *iter ) )
  14. {
  15. cout << iter->native_directory_string() << " (directory)\n" ;
  16. if( recurse_into_subdirs ) show_files(*iter) ;
  17. }
  18. else
  19. cout << iter->native_file_string() << " (file)\n" ;
  20. }
  21. }
  22.  
  23. int main()
  24. {
  25. show_files( "/usr/share/doc/bind9" ) ;
  26. }
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)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC