You are here:

C++/listing a directory


using turbo c++ 3.0
os : xp pro

  How do i find the contents of the current folder in a c++ program? I want to list the contents as well as enable the program to make decisions concerning the contents.

ie: when prompting which file to save in, i want to list all available contents in the current directory and let the user decide which file to use. system("dir") only lists the contents.

The quick answer is that there is no standard C++ or C way to work with file system directories (or folders if you prefer).

To find out what items are in a directory you have to use operating specific functions. So you have to look at what the Win32 API supplies. You also have to have the MS platform SDK installed or 3rd party equivalent for your compiler.

The reason for the SDK or 3rd party equivalent is to obtain the Windows API header files and libraries to link against. One such 3rd party set of libraries are the MinGW libraries (see

The problem here is that Turbo C++ 3.0, as far as I can tell from a Google search, is an old 16-bit DOS compiler. Hence it cannot use the native Win32 API, you would have to use the ancient DOS API which I no longer use but involved loading 16-bit real mode X86 registers with command codes and parameter values and mainly performing operating system service requests through an INT 21h instruction. The services with codes 4Eh and 4Fh (find first matching file and find next matching file) are probably what you need. Explaining how do use the MS-DOS API further would take to long and has little if anything to do with C++.

If you are lucky Turbo C++ may have included higher level support for such operations in some (non standard) library - maybe the compiler's C runtime library - check the documentation for the product to see what is available.

If I am correct and you do have an old 16-bit DOS targeted compiler then you might like to upgrade to something newer (and 32-bit Windows friendly!). One example would (again) be the MinGW Windows port of GCC + Win32 libraries and headers - a good free development environment based on these is Dev-C++ (see

Having obtained the ability to use the Win32 API in your programs you can use Win32 API functions for folder manipulation. Here are some you might find useful:

FindFirstFile, FindFirstFileEx, FindNextFile, FindClose : These are the base functions for iterating through the files in a directory. You start with FindFirstFile or its extended version FindFirstFileEx, continue with FindNextFile until GetLastError returns ERROR_NO_MORE_FILES and finish by calling FindClose.

GetOpenFileName, GetSaveFileName : These functions display the Windows common open and save file name dialog boxes allowing the user to select the item they wish.

You can find further information on the Win32 API functions in the MSDN library which can be found online at

I should mention that the Boost libraries (see contain a C++ wrapper for various OS specific API functions such as the Windows FindFirstFile, FindNextFile, FindClose functions in their file system library - see for further details. MS-DOS is probably not supported though.


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


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.


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


©2016 All rights reserved.