listing all files in a directory

D

David Jacques

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 ?
 
J

Joona I Palaste

David Jacques said:
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.
 
F

Flash Gordon

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.
 
S

SM Ryan

# 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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top