By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,079 Members | 926 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,079 IT Pros & Developers. It's quick & easy.

Opening directories

P: n/a

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
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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!


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?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

P: n/a
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
Jul 22 '05 #3

P: n/a
Fred H <se****@nospam.com> writes:
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).
--
Roger Leigh

Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #4

P: n/a
In article <87************@wrynose.whinlatter.uklinux.net>, ${roger}
@invalid.whinlatter.uklinux.net.invalid says...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #5

P: n/a

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 ;)
Jul 22 '05 #6

P: n/a

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


Thanks. This seems to be exactly what I need. I hope... :-)
Jul 22 '05 #7

P: n/a
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 :)
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.