Reading directories: UNIX vs. Windows/VC++ 
Author Message
 Reading directories: UNIX vs. Windows/VC++

Hi,

  I have an application which needs to go to a particular directory
and find and process all the files that have names that match a
particular pattern.

  I have done this in UNIX using opendir() to open the directory and a
series of readdir() calls to look at each of the filenames that it
contains.

  I am trying to do the same thing for a Windows NT application using
Visual C++.  However, I don't believe the opendir or readdir functions
exist in the Visual C++ libraries, and I am not able to find any
equivalent functions.

  Would anyone have any ideas on how to go about solving this problem?

  Thanks,

  Lee

  lng (at) utdallas (dot) edu



Fri, 25 Aug 2000 03:00:00 GMT  
 Reading directories: UNIX vs. Windows/VC++

Quote:

>Hi,

>  I have an application which needs to go to a particular directory
>and find and process all the files that have names that match a
>particular pattern.

>  I have done this in UNIX using opendir() to open the directory and a
>series of readdir() calls to look at each of the filenames that it
>contains.

>  I am trying to do the same thing for a Windows NT application using
>Visual C++.  However, I don't believe the opendir or readdir functions
>exist in the Visual C++ libraries, and I am not able to find any
>equivalent functions.

>  Would anyone have any ideas on how to go about solving this problem?

From the C FAQ:
"19.20:  How can I read a directory in a C program?

A:      See if you can use the opendir() and readdir() routines, which
        are part of the POSIX standard and are available on most Unix
        variants.  Implementations also exist for MS-DOS, VMS, and other
        systems.  (MS-DOS also has FINDFIRST and FINDNEXT routines which
        do essentially the same thing.)  readdir() only returns file
        names; if you need more information about the file, try calling
        stat().  To match filenames to some wildcard pattern, see
        question 13.7.

        References: K&R2 Sec. 8.6 pp. 179-184; PCS Sec. 13 pp. 230-1;
        POSIX Sec. 5.1; Schumacher, ed., _Software Solutions in C_
        Sec. 8."

That having been said, Bob Stout's Snippets collection probably has code that
does what you want.  Or you can explore a newsgroup specific to your needs for
a [possibly] superior solution such as

--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com



Fri, 25 Aug 2000 03:00:00 GMT  
 Reading directories: UNIX vs. Windows/VC++

Quote:

>   I have an application which needs to go to a particular directory
> and find and process all the files that have names that match a
> particular pattern.

>   I have done this in UNIX using opendir() to open the directory and a
> series of readdir() calls to look at each of the filenames that it
> contains.

>   I am trying to do the same thing for a Windows NT application using
> Visual C++.  However, I don't believe the opendir or readdir functions
> exist in the Visual C++ libraries, and I am not able to find any
> equivalent functions.

        If you have the specific file name, Win32 makes this very easy with
the SearchPath() function which is part of the API and should be usable from
in Visual C code (just include windows.h),

        char fileName[MAX_PATH];
        char pathName[MAX_PATH];
        char resultPath[MAX_PATH];
        char *pos;
        DWORD result;

        :
        result = SearchPath( pathName, fileName, 0, MAX_PATH, resultPath, pos);
        if (result == 0) {
                /* File was not found or there was an error, */
                /* Call GetLastError(). */
        } else {
                /* fileName was found.  resultPath contains full filespec, */
                /* pos points to first character of found filename. */
        }
        :

        Replace passing pathName with passing NULL as the first argument to
SearchPath, and it will systematically search directories (see documentation.)

        Note that SearchPath does have limitations, it stops on the first file
it finds, and cannot take wildcards.

        Overcoming these problems requires using FindFirstFile() and
FindNextFile() in each directory, as the following sample suggests for use
in the current directory,

        HANDLE hFile;  /* cannot use this handle with CreateFile() */
        WIN32_FIND_DATA fData;
        char fileMask[64];

        :
        /* Initialize fileMask, may also be passed as a parameter. */
        stcpy( fileMask, "*.INI");
        :
        /* Get First File Data matching fileSpec. */
        hFile = FindFirstFile( fileMask, &fData);
        if ( hFile != INVALID_HANDLE_VALUE ) {

                /* Process this file using information from fData. */

                /* Iterate through remaining file matchs. */
                while ( FindNextFile( hFile, &fData ) ) {

                        /* Process this file using info from fData. */

                }

        }
        /* Perform necessary clean-up so the OS can release this handle. */
        FindClose( hFile);
        :

        Of course, you can use SetCurrentDirectory() to move between directories
as necessary.  See the API documentation for more information on the WIN32_FIND_
DATA structure, you can get the full name from it and open it normally if nec-
cessary.

        In the future, you can probably expect a faster and more thorough re-
sponse to this question if you post it to a Windows specific programming news-

                                                        -- Stone



Fri, 25 Aug 2000 03:00:00 GMT  
 Reading directories: UNIX vs. Windows/VC++

: Hi,

:   I have an application which needs to go to a particular directory
: and find and process all the files that have names that match a
: particular pattern.

:   I have done this in UNIX using opendir() to open the directory and a
: series of readdir() calls to look at each of the filenames that it
: contains.

:   I am trying to do the same thing for a Windows NT application using
: Visual C++.  However, I don't believe the opendir or readdir functions
: exist in the Visual C++ libraries, and I am not able to find any
: equivalent functions.

:   Would anyone have any ideas on how to go about solving this problem?

Port a dirent() library; AFAIK there are several, and I've no doubt
it's been done before, so dig around before putting any work in.

Will



Sat, 26 Aug 2000 03:00:00 GMT  
 Reading directories: UNIX vs. Windows/VC++



Quote:
>  I am trying to do the same thing for a Windows NT application using
>Visual C++.  However, I don't believe the opendir or readdir functions
>exist in the Visual C++ libraries, and I am not able to find any
>equivalent functions.

Last time I looked, opendir() et al were provided with Visual C
and Borland C (don't know about Waterloo C).


Sat, 26 Aug 2000 03:00:00 GMT  
 Reading directories: UNIX vs. Windows/VC++

Quote:

>         If you have the specific file name, Win32 makes this very easy with
> the SearchPath() function which is part of the API and should be usable from
> in Visual C code (just include windows.h),

[SNIP]

Just as a note of caution: this is all *very* system and compiler
specific stuff. "windows.h" is not part of standard C and neither
are the "SearchPath()" or the "FindFirst()" functions. Actually
standard C does not support reading the contents of directories.

Quote:
>         In the future, you can probably expect a faster and more thorough re-
> sponse to this question if you post it to a Windows specific programming news-


I second that notion.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sat, 26 Aug 2000 03:00:00 GMT  
 Reading directories: UNIX vs. Windows/VC++

Quote:


> >         If you have the specific file name, Win32 makes this very easy with
> > the SearchPath() function which is part of the API and should be usable from
> > in Visual C code (just include windows.h),
> [SNIP]

> Just as a note of caution: this is all *very* system and compiler
> specific stuff. "windows.h" is not part of standard C and neither
> are the "SearchPath()" or the "FindFirst()" functions. Actually
> standard C does not support reading the contents of directories.

At the risk of starting a flame war, may I suggest that while the
standard
may not provide portable services for reading the contents of
directories,
cursor positioning, or any other environment-specific notions, the
language
does not prohibit the implementation of such.

Does that make sense?

Quote:

> >         In the future, you can probably expect a faster and more thorough re-
> > sponse to this question if you post it to a Windows specific programming news-

> I second that notion.

I also agree.  The problem has been that when one learns from a
book/instructor
that does not distinguish between language and environment, the student
has no
idea that there is a difference.

Quote:

> Stephan
> (initiator of the campaign against grumpiness in c.l.c)

Mike
--
R. Michael "Okie" Gillmore

    "Happiness, like unhappiness, is a pro-active choice."
                        -- Stephen R. covey



Sat, 26 Aug 2000 03:00:00 GMT  
 Reading directories: UNIX vs. Windows/VC++

On Tue, 10 Mar 1998 08:24:23 -0600, R. Michael Gillmore

Quote:



>> >         If you have the specific file name, Win32 makes this very
>> > easy with the SearchPath() function which is part of the API and
>> > should be usable from in Visual C code (just include windows.h),
>> [SNIP]
>> Just as a note of caution: this is all *very* system and compiler
>> specific stuff. "windows.h" is not part of standard C and neither
>> are the "SearchPath()" or the "FindFirst()" functions. Actually
>> standard C does not support reading the contents of directories.

Derek does point the the person who asked to the correct newsgroup
below.

Quote:
>At the risk of starting a flame war, may I suggest that while the
>standard may not provide portable services for reading the contents
>of directories, cursor positioning, or any other environment-specific
>notions, the language does not prohibit the implementation of such.

I know of no strictly conforming implementations for entering a
subdirectory of a filesystem.  Indeed, there are even C platforms for
which there aren't any directories.

However, this question has been asked often enough that it is in fact
answered by the C-faq.  I always recommend reading the C-faq in its
entirety to all C programmers, since it provides many tips for
avoiding pitfalls and solutions to commonly faced problems.  However,
this particular problem is addressed by question 19.20.

  http://www.eskimo.com/~scs/C-faq/top.html

Quote:
>Does that make sense?
>> >         In the future, you can probably expect a faster
>> > and more thorough re- sponse to this question if you post
>> > it to a Windows specific programming news- group, such as

>> I second that notion.

Or read the C-faq.

Quote:
>I also agree.  The problem has been that when one learns from a
>book/instructor that does not distinguish between language and
>environment, the student has no idea that there is a difference.

Which is why it is all the more important to point out the differences
here.  Why confuse a poor newbie who attempts to do this with his
DJGPP compiler and fails horribly?

"Read the C-faq.  I can't believe it's not butter."

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Sat, 26 Aug 2000 03:00:00 GMT  
 Reading directories: UNIX vs. Windows/VC++

Quote:

>Hi,

>  I have an application which needs to go to a particular directory
>and find and process all the files that have names that match a
>particular pattern.

>  I have done this in UNIX using opendir() to open the directory and a
>series of readdir() calls to look at each of the filenames that it
>contains.

>  I am trying to do the same thing for a Windows NT application using
>Visual C++.  However, I don't believe the opendir or readdir functions
>exist in the Visual C++ libraries, and I am not able to find any
>equivalent functions.

>  Would anyone have any ideas on how to go about solving this problem?

FirstFirstFile ()
FindeNextFile ()

John - N8086N
Organizer and Executive Vice President of the
"Vast Right-Wing {*filter*}"
The Vast Right-Wing {*filter*} is now on-line at
http://home.att.net/~miano/{*filter*}.htm
------------------------------------------------
EMail Address:

|c.o.l.o.s.s.e.u.m.b.u.i.l.d.e.r.s.|
|c.o.m.|

Full Name:
-------------------
-J.o.h.n?M.i.a.n.o-
-------------------



Sat, 26 Aug 2000 03:00:00 GMT  
 Reading directories: UNIX vs. Windows/VC++

Shashamane!
Please look at the signature for my actual email address...

Quote:

>:   I am trying to do the same thing for a Windows NT application using
>: Visual C++.  However, I don't believe the opendir or readdir functions
>: exist in the Visual C++ libraries, and I am not able to find any
>: equivalent functions.

check if findfirst() and findnext() are available.

              Kumasee
              Barninga Z!


Home:  http://barninga.base.org
ZMK:   http://zmk.base.org
Roxy:  http://rossanacasale.base.org



Sat, 26 Aug 2000 03:00:00 GMT  
 Reading directories: UNIX vs. Windows/VC++



Quote:
>At the risk of starting a flame war, may I suggest that while the standard
>may not provide portable services for reading the contents of directories,
>cursor positioning, or any other environment-specific notions, the language
>does not prohibit the implementation of such.

Indeed, the POSIX standard interface includes them.

I was considering proposing the POSIX <dirent.h> facilities for standardization
in C9x, but by the time I identified all the caveats, there wasn't enough left
to be terribly useful.  I still think some such interface could be standardized,
but to be useful enough to be generally successful, it would have to differ from
the <dirent.h> facilities in numerous ways, most notably in providing more info
about each entry (e.g., entry type, length if known, ...).  Probably a general
ftw()-like facility (but not ftw() itself, too flawed) would be better than
merely a "get next entry" approach.



Sun, 27 Aug 2000 03:00:00 GMT  
 Reading directories: UNIX vs. Windows/VC++

The Win32 equivalent functions are:

FindFirstFile(...)
FindNextFile(...)

You can pass a pattern to search on. It is easy to use
Steve



Sun, 27 Aug 2000 03:00:00 GMT  
 Reading directories: UNIX vs. Windows/VC++



Quote:


>> >         If you have the specific file name, Win32 makes this very easy with
>> > the SearchPath() function which is part of the API and should be usable from>> > in Visual C code (just include windows.h),
>> [SNIP]

>> Just as a note of caution: this is all *very* system and compiler
>> specific stuff. "windows.h" is not part of standard C and neither
>> are the "SearchPath()" or the "FindFirst()" functions. Actually
>> standard C does not support reading the contents of directories.

>At the risk of starting a flame war, may I suggest that while the
>standard
>may not provide portable services for reading the contents of
>directories,
>cursor positioning, or any other environment-specific notions, the
>language
>does not prohibit the implementation of such.

However there is no way to implement such things in C. Any such
implementation will depend on platform related facilities beyond what
C provides and will therefore be platform-specific. It makes no sense to
discuss things like that in a platform-independent newsgroup such as
comp.lang.c. There are a huge number of platform related newsgroups
available for this.

...

Quote:
>I also agree.  The problem has been that when one learns from a
>book/instructor
>that does not distinguish between language and environment, the student
>has no
>idea that there is a difference.

Which is why we go to great length to draw their attention to the difference.

--
-----------------------------------------


-----------------------------------------



Sun, 27 Aug 2000 03:00:00 GMT  
 Reading directories: UNIX vs. Windows/VC++


Quote:

>The Win32 equivalent functions are:

>FindFirstFile(...)
>FindNextFile(...)

>You can pass a pattern to search on. It is easy to use
>Steve

<humor>

This is a remarkably well designed interface, completely unlike that of arcane
MS-DOS.

It is patterned after the Microsoft standard C library, in which one writes:

        FILE *f;
        char c;

        if (getfirstchar(&f, &c, "file", "r+") == 0) {
                /* .. could not open file .. */
        } else {
                do {    
                        /* ... process value in c .. */
                } while (getnextchar(f, &c));
        }

Rumors have it that Steve Maguire had his fingers in this, in an attempt to
improve the solidity of code, after which he soon exercised the liquidity
of his company stock.

</humor>



Sun, 27 Aug 2000 03:00:00 GMT  
 Reading directories: UNIX vs. Windows/VC++

Quote:

> check if findfirst() and findnext() are available.

Why do you suggest this? These functions are not part of C.

--
(initiator of the campaign for grumpiness where grumpiness is due in c.l.c)

Attempting to write in a hybrid which can be compiled by either a C compiler
or a C++ compiler produces a compromise language which combines the drawbacks
of both with the advantages of neither.



Sun, 27 Aug 2000 03:00:00 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Reading directories: UNIX vs. Windows/VC++

2. Reading directories under unix...

3. QUERY: How to read directories in UNIX C...?

4. Reading binary files with C Linux vs Unix

5. Who makes more $$ - Windows vs. Unix p

6. Unix vs Windows Line Endings

7. Memory Management (Windows vs. Unix)

8. Need to read a directory structure on an Windows computer

9. Good Sources for UNIX vs. MS VC++ Libraries

10. VS.NET IDE VC++ Directories

11. open/read/close directory, vc++5

12. Windows ReadFile vs. MM Read functions

 

 
Powered by phpBB® Forum Software