-2

Is there a way to read file names from a folder using purely C (or 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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Tony R
  • 11,224
  • 23
  • 76
  • 101
  • This is an exact duplicate to your other question, you've just added the "no FindFirstFile()." Try not to add redundant questions. – Evan Teran Mar 04 '09 at 05:46
  • see this solution to your previous question as well: http://stackoverflow.com/questions/609203/reading-file-names/609236#609236. Like the rest of us, he recommends boost. – Evan Teran Mar 04 '09 at 05:47

4 Answers4

10

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
  • 87,561
  • 32
  • 179
  • 238
4

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

dirkgently
  • 108,024
  • 16
  • 131
  • 187
0

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
  • 11,397
  • 8
  • 56
  • 61
  • Unfortunately he didn't listen the first time you gave him that answer :-P. – Evan Teran Mar 04 '09 at 05:50
  • These functions aren't available on Windows, since Windows is not POSIX. They are available under Cygwin, however. – Adam Rosenfield Mar 04 '09 at 05:51
  • 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 Mar 04 '09 at 05:54
  • 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 Mar 04 '09 at 14:13
0

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
  • 23,735
  • 11
  • 56
  • 82
  • 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 Mar 04 '09 at 14:15
  • 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 Mar 04 '09 at 14:31