?
Solved

Reading directories in C++

Posted on 2003-03-29
7
Medium Priority
?
1,048 Views
Last Modified: 2012-05-04
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.
0
Comment
Question by:Ubercat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8231660
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
 

Author Comment

by:Ubercat
ID: 8232418
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
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8232511
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Ubercat
ID: 8232663
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
 
LVL 8

Accepted Solution

by:
fl0yd earned 375 total points
ID: 8232713
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
 

Author Comment

by:Ubercat
ID: 8232911
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
 
LVL 8

Expert Comment

by:fl0yd
ID: 8232944
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question