Link to home
Start Free TrialLog in
Avatar of Ubercat
Ubercat

asked on

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.
Avatar of bkrahmer
bkrahmer

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
Avatar of Ubercat

ASKER

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.
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
Avatar of Ubercat

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of fl0yd
fl0yd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ubercat

ASKER

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.
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