parsing directory

O

oswald.harry

hi all,
i am a newbie to c and learning by K&R..
i need to parse a folder in my system and create an array containing
the fully qualified names of all .png files in sorted order ,ie in
ascending order of filenames..i have been struggling for sometime..:-
( but i don't know how to open a directory when the dir name is
given as a string

ie i would call
myapp.exe F:\mycode\mypngfiles\

any advise ,pointers wd be most appreciated
thank you
hos.
 
J

Jensen Somers

hi all,
i am a newbie to c and learning by K&R..
i need to parse a folder in my system and create an array containing
the fully qualified names of all .png files in sorted order ,ie in
ascending order of filenames..i have been struggling for sometime..:-
( but i don't know how to open a directory when the dir name is
given as a string

ie i would call
myapp.exe F:\mycode\mypngfiles\

any advise ,pointers wd be most appreciated
thank you
hos.

Take a look at the functionality included in <dirent.h>. This header
files declares functions to browse a directory and lists its content and
is, if I recall correctly, also mentioned in the K&R book. However, I
can't say for sure this functionality is available on MS Windows using
the MS Visual C compiler. Maybe you'll need to find some WINAPI
functions that mimic <dirent.h> behavior.

If you have the list of file, you only need to do a string comparison on
the file extension. To be a 100% safe you should take a look at the MIME
settings of the file because on Unix for example file extensions are not
mandatory. This is however outside the scope of this discussion group.

- Jensen
 
M

Mark Bluemel

hi all,
i am a newbie to c and learning by K&R..
i need to parse a folder in my system and create an array containing
the fully qualified names of all .png files in sorted order ,ie in
ascending order of filenames..i have been struggling for sometime..:-
( but i don't know how to open a directory when the dir name is
given as a string

ie i would call
myapp.exe F:\mycode\mypngfiles\

any advise ,pointers wd be most appreciated

You should probably look on a Micro$oft-specific newsgroup, or even
their website.

Standard C doesn't even have the concept of directories. Any access to
directories/folders/etc will be via non-standard (platform-supplied)
functionality.
 
M

Malcolm McLean

i am a newbie to c and learning by K&R..
i need to parse a folder in my system and create an array containing
the fully qualified names of all .png files in sorted order ,ie in
ascending order of filenames..i have been struggling for sometime..:-
( but i don't know how to open a directory when the dir name is
given as a string

ie i would call
myapp.exe F:\mycode\mypngfiles\
Looks like you are on a DOS / Windows system.
Windows provides a non-standard set of functions called opendir() and
readdir(). Basically you open the directory, then read it until you get a
null result, indicating that theb last file has been read.

So for the part of your system that can be portable.
1) You need to check that the file extension is .png. If not, discard it.
2) If it is .png, put it into an array (char **) you expand with realloc().
3) Write a function that sorts the filenames, and pass to qsort().

One quirk of ASCII is that if filenames contain embedded numbers they may
not sort as you would like. However there is function, designed for qsort()
on my website which overcomes this problem. It also provides a function to
get the extension - not a hard problem, but you have to handle a few special
cases like the upper directory being listed as ..
 
R

Richard Heathfield

(e-mail address removed) said:
hi all,
i am a newbie to c and learning by K&R..
i need to parse a folder in my system and create an array containing
the fully qualified names of all .png files in sorted order ,ie in
ascending order of filenames..i have been struggling for sometime..:-
( but i don't know how to open a directory when the dir name is
given as a string

ie i would call
myapp.exe F:\mycode\mypngfiles\

any advise ,pointers wd be most appreciated

Others have already mentioned that this is off-topic here, so I won't
bother. I heartily recommend comp.os.ms-windows.programmer.win32 to you,
but just in case you're in a tearing hurry, you may want to check this
out:

http://www.cpax.org.uk/prg/windows/dirparse.c

Here is an approximate Linux equivalent, for comparison:

http://www.cpax.org.uk/prg/linux/dirwalk.c

As you will see if you chase those links, they are rather different!
(That's why we tend to refer such questions to platform-specific groups.)
 
F

Flash Gordon

Malcolm McLean wrote, On 21/01/08 16:19:
Looks like you are on a DOS / Windows system.
Windows provides a non-standard set of functions called opendir() and

<snip>

Alternatively ask on a Windows group as others suggested where they may
point out another easier solution.
 
U

user923005

hi all,
i am a newbie to c and learning by K&R..
i need to parse a folder in my system and create an array containing
the fully qualified names of all .png files in sorted order ,ie in
ascending order of filenames..i have been struggling for sometime..:-
(   but i don't know how to open a directory when the dir name is
given as a string

ie i would call
myapp.exe F:\mycode\mypngfiles\

Probably, you want this:
http://legacy.imatix.com/html/sfl/
and in particular this:
http://legacy.imatix.com/html/sfl/sfl229.htm#TOC242

It's OT for of course.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top