Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Reading directories in C++

Posted on 2003-03-29
7
Medium Priority
?
1,051 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

580 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