Thread: Directory listing?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    1

    Directory listing?

    Hello everyone!

    My first post here.

    GNU's file management reference isn't clear enough for me:
    http://www.delorie.com/gnu/docs/glib...l#SEC_Contents

    I get how to recursively get each file/directory name inside a directory, but the question is: how do I get the NUMBER OF files/dirs, so I can allocate an array to store these filenames?

    Thanks in advance, people!

    (P.S: I'm not coding for Windows, son don't give me Windows API. I'm coding for, guess, what, the Sony PSP. )

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > how do I get the NUMBER OF files/dirs, so I can allocate an array to store these filenames?
    At least one example shows how to allocate an expanding array as you go. Perhaps adapt the idea for your own circumstance.
    http://faq.cprogramming.com/cgi-bin/...&id=1044780608

    > I'm coding for, guess, what, the Sony PSP
    Then you'll need to rummage through the API for that machine then.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you need to store ALL filenames, or just some of them?

    If you need to store all of them, obviously, you need to use some strategy along the lines of expanding array, or some other dynamic storage method (linked list of a bunch of filenames, perhaps - a linked list of each name may be a bit too much overhead in the form of links and such). What method to use will also depend on what you are planning to do with the files after you've retrieved them from the file-system.

    If you need to store only some of them (and there are only really a few) then you may find that a linked list or a static oversized list (e.g. you expect there to be max 50 files, so you make an array that can hold 256 files) is the right approach.

    If you use a dynamicly growing array method, make sure you start with a decent size and make sure you grow it in large enough steps, so you don't get too much overhead from copying the content.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. On reading Directory contents and listing them.
    By Deathfrost in forum C Programming
    Replies: 9
    Last Post: 07-14-2009, 08:21 AM
  2. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  3. Listing specific files in a directory
    By knirirr in forum C Programming
    Replies: 14
    Last Post: 01-29-2008, 05:42 AM
  4. recursive directory listing help
    By laney69er in forum C Programming
    Replies: 2
    Last Post: 03-13-2006, 01:07 PM
  5. Directory listing
    By brif in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2002, 06:44 AM