Reading directories in C++

I am trying to find a way that a C++ console program can opena directory and red its contents, that is, read the filenames contained therein.  This program is going to be run on Windows, which is my problem.  I know there is a dirent.h file for UNIX, that has open directory and read directory functions, but I am having problems finding such a resource for a windows program.  Can anyone help?

The purpose for reading this directory is to retrieve the filenames and store them as character strings in a linked list.

I believe there are Win32 programs that cn also do that, but they use all the long words and whatnot. I'm only just now getting into Win32 programming.
UbercatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bkrahmerCommented:
The recommended way in windows is to use the findfirst, findnext, findclose SDK functions.  From the MSDN:

The following code shows a minimal use of FindFirstFileEx. This program is the equivalent of the example shown in FindFirstFile.

#define _WIN32_WINNT 0x0400

#include "windows.h"

int
main(int argc, char *argv[])
{
  WIN32_FIND_DATA FindFileData;
  HANDLE hFind;

  printf ("Target file is %s.\n", argv[1]);

  hFind = FindFirstFileEx(argv[1], FindExInfoStandard, &FindFileData,
                 FindExSearchNameMatch, NULL, 0 );

  if (hFind == INVALID_HANDLE_VALUE) {
    printf ("Invalid File Handle. Get Last Error reports %d\n", GetLastError ());
  } else {
    printf ("The first file found is %s\n", FindFileData.cFileName);
    FindClose(hFind);
  }

  return (0);
}


brian
0
UbercatAuthor Commented:
Thanks for the suggestion, but I need to know, is this to find a specific file?  Because what I'm trying to do is find out how to access a directory listing in a directory.  Is the argv[1] argument supposed to be a directory, or is it a filename?  Because I'm not trying to find a specific file.  This routine would presume not to know what the contents of the directory is, and it shoudl be able to read in all of the filenames, not just one file.
0
bkrahmerCommented:
The parameter taken is the search specification.  If you want to find all files in c:\temp, you'd pass in c:\temp\*.*.

To see if a file is a directory, you can check its attributes.

if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) // don't care about directories

brian
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

UbercatAuthor Commented:
I tried out the code, and the first file found was always the traget folder.  If the path was C:\cproj\BAA, trying to access the BAA folder, it would report the first file find as BAA, the folder itself. I'm trying to get at the filenames inside the folder.

I know on UNIX you can open a directory with a dirent.h function and then read its contents using an opendir function,a nd then a readdir function which cycles through all of the directory entries until it reaches the end, returning a null value. (So you can stick it in a loop.)  That's what I'm trying to do here.
0
fl0ydCommented:
You need to call FindNextFile repeatedly after you called FindFirstFile[Ex] to iterate through an entire directory. If you found another directory, call your function again with this directory as its argument to recursively move down all subdirectory paths. Make sure though that you check for the 'symbolic' directory entries '.' [current directory] and '..' [parent directory] to avoid infinite loops.

In contrary to UNIX there are no specific functions on Windows systems that only deal with directories. That's why dirent.h is a platform specific implementation and not portable to other systems [apart from the fact that it's c anyway].

.f
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
UbercatAuthor Commented:
Thanks.  I finally found the routine in a Win32 book I was going through.  It had the FindNextFile routine for basically creating a Directory like function, which is exactly what I was looking for.

The book I found it in, by the way, was Win32 System Services by Marshall Brain. (What a surname, eh?)  It's a Prentice Hall book.
0
fl0ydCommented:
Marshall Brain :) I bet he must have had a hell of a time in school.

Anyway, books are good to learn about concepts. They aren't that great as a reference, though, especially cross-references are cumbersome to maneuver through. I would suggest the MSDN's online version when diving into Windows programming. In case you don't have the link already: http://msdn.microsoft.com/library/default.asp

.f
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.