You are here:

C++/Listing Files in a folder

Advertisement


Question
Hi Ralph,

I want to know, how can I list down the files in a folder in C++ (NOT MFC). I am making a client server program where, after the client is connected to server, client get a list of files that can be downloaded from the server side. So can you help in that?

Regards,
Yousaf

Answer
First I am sorry for not getting back to you sooner. I have been unexpectedly away from home for a few days and have only just now got access to the internet.

There is no standard way to iterate through a folder (or directory) in C and C++. You have to use the operating system facilities to do this or use facilities in a 3rd party library such as the directory iterator of the Boost file system library see http://www.boost.org/ and http://www.boost.org/libs/filesystem/doc/index.htm.

On UN*X and Linux like systems you use the dir family of functions such as opendir, closedir and readdir and the DIR type. You can find out about them using the man pages or UN*X/Linux programming text.

On MS Win32 systems such as Windows XP you use the FindFile family of Win32 SDK functions such as FindFirstFileEx, FindNextFile and CloseFind. You can find out about them in the MSDN library documentation, which is online at msdn.microsoft.com and http://msdn.microsoft.com/library/.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.