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 );
}
}
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
> 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
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.
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.
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?
> 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.
Cable_TXG
Tonny Gregersen
--
Paul Lutus
www.arachnoid.com
Tonny Gregersen <ton...@control.auc.dk> wrote in message
news:ebox4.3387$ME6....@twister.sunsite.auc.dk...
>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
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
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>