You are here:

C++/regarding directory structure



I have a small question.
How do I compute the number of files(which are specific such as .doc) in a directory in C++.

For example, if I have 10 files of type .doc format in a directory, how do I compute this value using C++.

Any help is appreciated.


In a naive manner, you can do the following: Use system() command to write the number of files in a temporary file for example, system("ls *.doc|wc -w > /tmp/number_of_doc_files"). Then open and read the temporary file to obtain the result and clear it.

Since C++ does not support directory traversal within the language libraries, this functionality will be part of operating system (OS) libraries. Search on google for windows/linux (according as the OS you are using) library for directory traversal.

There are third party libraries which provide directory traversal support on many platforms in a platform independent manner. One example is ACE (Adaptive Communication Environment). Boost also supports directory traversal, but I think ACE's implementation would be easier to include in your code.


All Answers

Answers by Expert:

Ask Experts


Amit Kumar


I can answer Cplusplus language and library questions, including STL, ACE, Boost. I have a good background in Mathematics.


Programming in CPP for about 8 years. Industry experience of 4 years.

Bachelors and Masters in Computer Science from Indian Institute of Technology Delhi.

©2017 All rights reserved.