Hi! How are you doing? I'd like to ask you a question if you have a bit of time. How do you use C (or C++ if C can't do it) to open a file if you don't know the name of the file? Here's what I want to do: I want my program to open a folder (c:\myfolder) and display all the filenames that are located in this folder and open these files for me. Thank you so much for you time. Have a great day.  Eric

I am a little confused as to why you are asking me (as a C++ expert) primarily about C! However in this case the simple answer is the same:

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 (for C++) the directory iterator of the Boost file system library see:


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 (which your question implies you are probably using from your reference to c:\myfolder) you use the FindFile family of Win32 SDK functions such as FindFirstFileEx, FindNextFile and FindClose. You can find out about them in the MSDN library documentation, which is online at:


The basic idea is that you start by calling FindFirstFile or similar, then call FindNextFile to iterate through the directory entries. When finished you call FindClose. For more information with some example code try starting with the MSDN library description of FindFirstFile at:


then follow links to the other functions of interest.

Hope you find this of use.


Oh, I should have pointed out that having got access to the names and other attributes of the files in a folder you need to ensure you build a proper path names (using the original folder path plus the file names returned from FindFirstFile, FindNextFile etc.) and then use these to open files. The attribute information you require most obviously is whether the returned name refers to an ordinary file or another directory. The WIN32_FIND_DATA structure used with the Find functions has both filename and file attribute fields. In particular the cFileName and the dwFileAttributes fields will be of interest. An attribute (flag) value of FILE_ATTRIBUTE_DIRECTORY unsurprisingly indicates the entry is for a directory and not an ordinary file. See:


For more information.

Finally, you will of course need the Microsoft platform SDK installed or a 3rd party implementation such as that offered by MinGW for the MinGW port of the GNU g++/gcc compiler (note I do not use this compiler so cannot say how good their Win32 API support is, nor exactly how it maps to the usual Microsoft supplied Windows SDK components).  


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 http://www.open-std.org/jtc1/sc22/wg21/.


©2017 About.com. All rights reserved.