Thread: Retrieving Filenames from a directory.

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Retrieving Filenames from a directory.

    system: Windows Pro SP2

    Question:
    How do I retrieve filenames from a given directory?

    Attempts:
    system("dir directory");
    I looked at the code in DWK's df program, but it was in C, and was very confusing.


    system("dir"); works but I don't want to have to make a parser for the file it creates.

    I was wondering what other possible solutions you all may have that could assist me.
    Thank you.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    From the FAQ.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Those are not really that helpful. I looked at them before, but they either find one file you specify, wrong OS, or they find all the files and list them, and they all use function I am not familiar with, such at strcat, and similar. I was wondering if there is something kind of like FindNextFile() that is not Win32?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If there was an easy manner to do this, it would make things much more simple.

    The problem is that files are maintained by an O/S in some manner specific to it. You can never really be sure how the files will be maintained and how you can interact with them. The C and C++ libraries are built so that they interact with the O/S in various manners. You can be sure of how the library functions will behave with some certainty, but you can never be sure of their implementation. For example, the way fopen() is written in C or the *fstream classes are written in C++ are totally different when interfacing down deep with Windows as opposed to *nix systems.

    The easiest manner is probably to use system() as you stated, but it's liable to risks, one of which is the possibly of being exploited in some manner. Depending upon how advanced you are with the the Windows API, I would consider using it.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I see, I know Windows API fairly well, I just hate the 200-300 lines of codes assiciated with such simple tasks. Thank you for your assistance though.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    boost filesystem might have a cleaner interface for what you need.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Raigne View Post
    Those are not really that helpful. I looked at them before, but they either find one file you specify, wrong OS, or they find all the files and list them, and they all use function I am not familiar with, such at strcat, and similar. I was wondering if there is something kind of like FindNextFile() that is not Win32?
    You say you're on XP Pro so what's the matter with using FindNextFile()?

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    >>You say you're on XP Pro so what's the matter with using FindNextFile()?
    Nothing, I was just trying to find a shorter way to do it. That is what I am using now. I just hate all the memory management, and stuff that goes along with it. Also would using dirent.h be okay?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dirent.h is POSIX standard, so you'll find it on Linux systems, and Windows ports of Linux compilers. Like GCC.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  3. Replies: 6
    Last Post: 07-30-2003, 03:08 AM
  4. Filenames in a directory
    By devour89 in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2003, 07:49 AM
  5. Replies: 2
    Last Post: 04-14-2002, 01:03 PM