Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

findfirst and findnext in ANSI-C++

528 views
Skip to first unread message

Tonny Gregersen

unread,
Mar 1, 2000, 3:00:00 AM3/1/00
to
Hi

I am trying to extract the filenames in a directory that has a certain
extension ( eg. *.html).
I know how to do it with findfirst and findnext. These functions can only be
used in windows, and i want to make my program universal so i also can use
it on other operatingsystems (eg. Linux).

I have been unable to find some functions ANSI-C that do the same as
findfirst and findnext. Is there anyone who knows a way to do this in ANSI-C
or ANSI-C++, so I can use it on any operatingsystem.

PS. The little test version i have made so far looks something like this:

#include <iostream.h>
#include <io.h>

void main( void ){

struct _finddata_t c_file;
long hFile;

/* Find first .html file in current directory */
if( (hFile = _findfirst( "*.html", &c_file )) == -1L ){
cout << "No *.html files in current directory!\n";
} else {
cout << "List of html files\n";
cout << c_file.name << "\n";

/* Find the rest of the .html files */
while( _findnext( hFile, &c_file ) == 0 )
{
cout << c_file.name << "\n";
}

_findclose( hFile );
}
}


Osah

unread,
Mar 1, 2000, 3:00:00 AM3/1/00
to
A point to note here:

1.) Directory implementation differs from one OS to another, therefore,
using findfirst and findnext on one OS, may not be valid on another OS.

:-)

Cable_TXG

Stephen Howe

unread,
Mar 1, 2000, 3:00:00 AM3/1/00
to

Tonny Gregersen <ton...@control.auc.dk> wrote in message
news:o97v4.775$ME6....@twister.sunsite.auc.dk...

> I have been unable to find some functions ANSI-C that do the same as
> findfirst and findnext. Is there anyone who knows a way to do this in
ANSI-C
> or ANSI-C++, so I can use it on any operatingsystem.

There aren't any. They do not supply OS functions in general. However POSIX
does.

POSIX functions opendir(), readdir(), closedir() are what you want.

Stephen Howe

Thomas_...@tecmar.com

unread,
Mar 1, 2000, 3:00:00 AM3/1/00
to
In article <89jd26$ebc$1...@news1.sunrise.ch>,

findfirst() and findnext() are not standard functions in either C or
C++. If you think they are, please quote the verse of the spec or a
reference manual.

For example, on my embedded platform, our OS doesn't support any
filesystems and doesn't have a findfirst() function.


Sent via Deja.com http://www.deja.com/
Before you buy.

Thomas_...@tecmar.com

unread,
Mar 1, 2000, 3:00:00 AM3/1/00
to
In article <89jd1k$6h4$1...@soap.pipex.net>,

Additional points:
1. The OP wanted functions that apply to "any operatingsystem{sic}".

2. Your functions only apply to OSes that support POSIX and a file
system.

3. There are no standard functions because not every operating system
supports a file system.

Thomas_...@tecmar.com

unread,
Mar 1, 2000, 3:00:00 AM3/1/00
to
In article <o97v4.775$ME6....@twister.sunsite.auc.dk>,

"Tonny Gregersen" <ton...@control.auc.dk> wrote:
> Hi
>
> I am trying to extract the filenames in a directory that has a certain
> extension ( eg. *.html).
> I know how to do it with findfirst and findnext. These functions can
only be
> used in windows, and i want to make my program universal so i also can
use
> it on other operatingsystems (eg. Linux).
>
> I have been unable to find some functions ANSI-C that do the same as
> findfirst and findnext. Is there anyone who knows a way to do this in
ANSI-C
> or ANSI-C++, so I can use it on any operatingsystem.
>
[program snipped]

Sorry, but there is no standard function for the simple fact that not
ALL operating systems support file systems.

Those OSes that do support file systems do not have the same structure.
Unix, DOS, VMS differ in the way the handle files and directories, to
name a few. There are many embedded OSes that do not have file systems.

How does your issue handle the case of an operating system that doesn't
support filesystems?

David Harmon

unread,
Mar 1, 2000, 3:00:00 AM3/1/00
to
On Wed, 01 Mar 2000 11:28:20 GMT in comp.lang.c++,
"Tonny Gregersen" <ton...@control.auc.dk> wrote:

> I have been unable to find some functions ANSI-C that do the same as
>findfirst and findnext.

That's right, there aren't any. For a relatively portable solution, see
Dietmar Kuehl's directory iterator class at www.boost.org.


Osah

unread,
Mar 2, 2000, 3:00:00 AM3/2/00
to
I never did once mention that they were part of the ANSI/Standard C/C++
Programming functions. I was just merely telling the poster why those
functions would differ from OS to OS and Compiler to Compiler. And yes i did
know it was OT.....

Cable_TXG

Tonny Gregersen

unread,
Mar 8, 2000, 3:00:00 AM3/8/00
to

Thanks! I'll look into it.
Though I think I'll make two different programmes, one for Linux\Unix and
one for Windows and combine these using ifdefine.

Tonny Gregersen

Paul Lutus

unread,
Mar 8, 2000, 3:00:00 AM3/8/00
to
Just write two different directory scanning routines within your program.
That way, the majority of the code is re-used.

--

Paul Lutus
www.arachnoid.com


Tonny Gregersen <ton...@control.auc.dk> wrote in message

news:ebox4.3387$ME6....@twister.sunsite.auc.dk...

Steve Alpert

unread,
Mar 9, 2000, 3:00:00 AM3/9/00
to
"Paul Lutus" <nos...@nosite.com> wrote:

>Just write two different directory scanning routines within your program.
>That way, the majority of the code is re-used.

There is an article in the April 2000 issue of C/C++ User's Journal entitled:
A Class for Scanning Directories

author claims it works on both Unix and MS-DOS

/steve

Steve Alpert (Steve_Alpert @ i d x . c o m)
IDX Systems Corp. Boston, Massachusetts


Tonny Gregersen

unread,
Mar 10, 2000, 3:00:00 AM3/10/00
to
Hi

I'm currently in Denmark, so I doubt I can get an issue of the jurnal.

Can I find the article online or can you give me a hint from the article
where to find an other description of the class for scanning directories.

Best regards
Tonny Gregersen

Dietmar Kuehl

unread,
Mar 10, 2000, 3:00:00 AM3/10/00
to ton...@control.auc.dk
Hi,
In article <%a4y4.4390$ME6....@twister.sunsite.auc.dk>,

"Tonny Gregersen" <ton...@control.auc.dk> wrote:
> Can I find the article online or can you give me a hint from the
> article where to find an other description of the class for scanning
> directories.

You can find an implementation of a class doing this online including
its documentation. Look for the directory iterator at
<http://www.boost.org/>. However, I would also be interested in this
article but I think it should be possible get the journal in Europe,
too.
--
<mailto:dietma...@claas-solutions.de>
homepage: <http://www.informatik.uni-konstanz.de/~kuehl>

0 new messages