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

listing all files in a directory

P: n/a
I am trying to get a list of all files of a certain extension type on disk
to
do some processing in a loop. The code needs to be portable to UNIX, so
I need to use plain c functionality. Does anyone know of a way to do this ?
Any URLs, code snippets, etc ?
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
David Jacques <Da***********@ccrs.nrcan.gc.ca> scribbled the following:
I am trying to get a list of all files of a certain extension type on disk
to
do some processing in a loop. The code needs to be portable to UNIX, so
I need to use plain c functionality. Does anyone know of a way to do this ?
Any URLs, code snippets, etc ?


You cannot do this in plain C. You cannot (at least not easily) do it as
portable between Windows and UNIX either. UNIX does have its own
specific functions for this, but those would be on-topic on
comp.unix.programmer and off-topic here.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"B-but Angus! You're a dragon!"
- Mickey Mouse
Nov 14 '05 #2

P: n/a
On Mon, 8 Nov 2004 07:35:25 -0500
"David Jacques" <Da***********@ccrs.nrcan.gc.ca> wrote:
I am trying to get a list of all files of a certain extension type on
disk to
do some processing in a loop. The code needs to be portable to UNIX,
so I need to use plain c functionality. Does anyone know of a way to
do this ? Any URLs, code snippets, etc ?


Unfortunately standard C does not include any directory handling, so you
will have to use implementation specific techniques. I would suggest
that comp.unix.programmer would be a good place to start. However, you
should first read the FAQ for that group and read at least a few days
worth of messages to get an idea as to what is acceptable.

If you want something portable to both Windows and Unix then you
have a bigger problem, since MS does things differently so you will need
2 versions of some of the code. However, that would still be off topic for this group.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #3

P: n/a
"David Jacques" <Da***********@ccrs.nrcan.gc.ca> wrote:
# I am trying to get a list of all files of a certain extension type on disk
# to
# do some processing in a loop. The code needs to be portable to UNIX, so
# I need to use plain c functionality. Does anyone know of a way to do this ?
# Any URLs, code snippets, etc ?

If the implementation includes a working system() function and the command
interpretter it calls provides this functionality, then you can start with
something like (for unix)

char command[REALLYBIGINTEGER];
sprintf(command,"/usr/bin/ls '%s' >.listing",directoryToBeListed);
system(command);
FILE *listing = fopen(".listing","r");
char line[REALLYBIGINTEGER];
while (fgets(line,listing)) {
...
}
fclose(listing);
system("/usr/bin/rm .listing");

modulo all the usual caveats about error checks and quote escapes, etc. I think
WIndows allows something similar but with command like 'DIR' or something.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
What kind of convenience store do you run here?
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.