tags:

views:

523

answers:

4

Hi,

Is there a way to read file names from a folder using purely C(++)? That means without including windows.h (no FindFirstFile(), etc...). It doesn't look like fstream has this functionality. I know that file names are operating system dependent, but I was hoping there is some library that will allow it in windows.

Thanks!

+10  A: 

boost filesystem is a nice solution. Of course under the hood, it will still be using the windows API calls (when you build on windows), but this is abstracted away from you.

Evan Teran
+3  A: 

C++ typically does not supply you with such functionality. A cross-platform solution is to use boost::filesystem.

dirkgently
A: 

Try the POSIX functions opendir() and readdir() for iterating through directories. See this link for the manual page with some great example code. These functions should be available on most platforms, both Windows and UNIX.

slacy
Unfortunately he didn't listen the first time you gave him that answer :-P.
Evan Teran
These functions aren't available on Windows, since Windows is not POSIX. They are available under Cygwin, however.
Adam Rosenfield
doesn't windows provide a small subset of posix functions as part of its (well, fail)-posix subsystem? i was never sure what the heck is part of that and what not.
Johannes Schaub - litb
windows NT provided a "posix subsystem" that gave limited compatibility with Posix. I believe that more recent versions have abandoned this and it was never well supported in the first place.
Jon Trauntvein
A: 

If you wish to use opendir() and readdir() on windows, you can download MinGW, a windows port of the famous GNU compiler collection. It includes windows ports of the UNIX header files, including dirent.h, which will allow you to use the specified functions. Keep in mind these will call native API's either way.

-John

John T
Actually, I believe that you would need to build on top of cygwin.dll in order for this to work. mingw merely provides a c/c++ compiler linked against the microsoft run-time.
Jon Trauntvein
That's what I said... it will call native Win32 API's either way but you don't have to use them in your code.
John T