Opening directories

F

Fred H

Hi

I've tried to find out if and how I can open
directories in standard C++, the same way I
can open files. To my great suprise, I haven't
managed to find out how this is done!

I'm using MS VC++ 6.0, so I know I could
use some Win specific classes, but I was
hoping to write portable code, and hence
I'm looking for some way of "opening" a
directory that's not platform or file system
dependant, since this code eventually will
be running on both Windows and Linux PCs.

So does such functions exist?

What I want to accomplish, is to be able to
make a very crude "file browser", that can
take some "root directory", and read its
subdirectories into a vector or something.
Then I can print a simple menu with "hit 1
to enter mydir", and so on. And when you've
entered the desired directory, you can display
files of some predefined extention, and run
them by hitting the appropriate menu item.

What it's all for? Feeding scripts to a test
bench. I'm writing a testbench for a hardware
design.

Any inputs would be greatly appreaciated.

Cheers
-Fred
 
K

Karl Heinz Buchegger

Fred said:
Hi

I've tried to find out if and how I can open
directories in standard C++, the same way I
can open files. To my great suprise, I haven't
managed to find out how this is done!

What is surprising you?
C++ doesn't have standard facilities to do that
and BTW. who has told you that a directory behaves
like a file?
 
D

Derek

Fred H wrote
Hi

I've tried to find out if and how I can open directories in
standard C++, the same way I can open files. To my great
suprise, I haven't managed to find out how this is done!

I'm using MS VC++ 6.0, so I know I could use some Win
specific classes, but I was hoping to write portable code,
and hence I'm looking for some way of "opening" a directory
that's not platform or file system dependant, since this
code eventually will be running on both Windows and Linux
PCs.

So does such functions exist?

What I want to accomplish, is to be able to make a very
crude "file browser", that can take some "root directory",
and read its subdirectories into a vector or something.
Then I can print a simple menu with "hit 1 to enter mydir",
and so on. And when you've entered the desired directory,
you can display files of some predefined extention, and run
them by hitting the appropriate menu item.

What it's all for? Feeding scripts to a test bench. I'm
writing a testbench for a hardware design.

Any inputs would be greatly appreaciated.

Cheers -Fred

The standard is silent on directories, so there is no way to
do what you want in Standard C++. However, the work of
porting a filesystem interface has already been done:

http://www.boost.org/libs/filesystem/doc/index.htm
 
R

Roger Leigh

Fred H said:
I've tried to find out if and how I can open
directories in standard C++, the same way I
can open files. To my great suprise, I haven't
managed to find out how this is done!

I'm using MS VC++ 6.0, so I know I could
use some Win specific classes, but I was
hoping to write portable code, and hence
I'm looking for some way of "opening" a
directory that's not platform or file system
dependant, since this code eventually will
be running on both Windows and Linux PCs.

So does such functions exist?

The portable way would be to use these POSIX functions: opendir(3),
closedir(3), readdir(3) and rewinddir(3).

Unfortunately, Microsoft OSes are about the only common ones not to
support POSIX. There are third party POSIX libraries for Windows
however (Cygwin and MinGW come to mind).


I've written a C++ class for this: mail me for details (remove the ${}
and invalids), or Google this group (I posted it a few weeks back).
 
J

Jerry Coffin

[ ... ]
The portable way would be to use these POSIX functions: opendir(3),
closedir(3), readdir(3) and rewinddir(3).

POSIX is "portable" in name, but in little else. ECMA 234 provides
functions for reading directories that I'm quite certain are portable to
a LOT more machines than POSIX (a large majority of machines that
support these POSIX functions also support the ECMA 234 functions, and
I'd bet there are far more ECMA 234/non-POSIX machines than there are
POSIX/non-ECMA 234 machines).
Unfortunately, Microsoft OSes are about the only common ones not to
support POSIX. There are third party POSIX libraries for Windows
however (Cygwin and MinGW come to mind).

Current Microsoft OSes (e.g. Windows NT, Windows 2000, Windows XP,
Windows 2003) all have a POSIX subsystem that, the last time I checked,
actually had a somewhat MORE accurate implementation of POSIX than many
of the systems more programmers are more likely to think of as
supporting POSIX (e.g. Linux, *BSD).

In all these cases, there are some fairly large areas where nearly all
the common systems are clearly deficient in implementing the full
current POSIX standard (e.g. none of the above appears to meet the
requirements for the real-time portion of the POSIX standard). At the
same time, opendir, closedir, readdir and rewinddir all work just as
well on Windows as they do on Linux, *BSD, etc.

Of course, you have to use the POSIX subsystem to do this -- these are
not part of Win32. You should not, however, mistake Win32 for being the
entire OS, by any means.
 
F

Fred H

What is surprising you?
C++ doesn't have standard facilities to do that

Well, that was what surprised me. As long as one can
manipulate files in standard C++, one really ought to
be able to manipulate directories too. Hence, when I
found no such standard way, I was surprised. After all,
it's a pretty common task.
and BTW. who has told you that a directory behaves
like a file?

Well, I know there are lots of ways to implement file
systems and physically store directory information to
disk, but in the sense that directories are chunks of
bytes on disk, the similarities with files are significant ;)
 
F

Fred H

Of course, you have to use the POSIX subsystem to do this -- these are
not part of Win32.

Thanks. I've tried to find out how I to use it, but haven't
managed to find out yet. Any input would be greatly appreaciated.
You should not, however, mistake Win32 for being the
entire OS, by any means.

I'm not sure I understand what you mean by this. Feel free to
enlighten me :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top