Thread: Platform independent way to traverse files in a directory

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    124

    Platform independent way to traverse files in a directory

    Greetings everyone

    This is a fairly complicated question, so just some general advice on where to look would be great. I don't expect someone to write this for me.

    Basically what I need is a platform independent way to do two things.

    1) Get the present working directory
    2) Sequentially open every file with a given extension in that directory.

    Platform independence is key. This is easy to do using Win32, but I need something that works across various flavors of Unix and Linux so it has to be standard ANSI C.

    Thanks all.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Have you tried googling your queery? It's supprising how many results one search can yeld.
    Double Helix STL

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > so it has to be standard ANSI C.
    There's no such thing in ANSI C

    The next level of portability is to stick to POSIX API calls, and here you will find things like opendir / readdir / closedir
    http://www.rt.com/man/opendir.3.html

    There is a POSIX API for win32.
    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.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Do I need to download a Posix API separately? Currently I'm developing under Visual Studio 6 and I can't find opendir or any related function in the MSDN.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Platform independence is key.
    You don't plan on distributing binaries and expecting them to be platform independent, right? So all you need to do is devise a wrapper that does what you want and link it with a platform dependent implementation for each port. It's actually pretty trivial to do, provided you know how to do what you want on each platform.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Quote Originally Posted by Prelude
    >Platform independence is key.
    You don't plan on distributing binaries and expecting them to be platform independent, right? So all you need to do is devise a wrapper that does what you want and link it with a platform dependent implementation for each port. It's actually pretty trivial to do, provided you know how to do what you want on each platform.
    The problem is that I don't know how to do what I need on every platform, that's why I just want to learn it one way that magically works across all platforms.

    And no, I don't plan to distribute one binary; I plan to distribute source code which will be recompiled.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I just want to learn it one way that magically works across all platforms.
    There's no silver bullet for something so platform dependent. You can look around and see if someone has already written a library that does what you want, otherwise you're stuck actually learning how to do it yourself. If something seems like magic, it's because somebody really smart tricked you into seeing it that way.

    >And no, I don't plan to distribute one binary
    I know. You're not clueless, so I wouldn't expect you to believe in that kind of magic.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Quote Originally Posted by Prelude
    >There's no silver bullet for something so platform dependent. You can look around and see if someone has already written a library that does what you want, otherwise you're stuck actually learning how to do it yourself. If something seems like magic, it's because somebody really smart tricked you into seeing it that way.
    So you don't think Salem's Posix idea will work? Dammit Prelude, you make me sad. Salem seems really smart and he tricked me into thinking that Posix would be my panacea. Your relentless cynicism has crippled my naive optimism in Salem's brilliance.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So you don't think Salem's Posix idea will work?
    No, I don't. As I understand it, you want this to work on Windows, Unix, and Linux. You said that Windows was easy but you don't know how to do it on the other systems. Since you're looking for a portable solution to save yourself the effort of learning the Unix/Linux way, you gain nothing because you still need to learn what the compatibility API implements, which is POSIX. On top of that, you also have to deal with the idiosyncrasies of how Windows implements POSIX. All in all, it's more work to use a portable API than it is to port the program as I suggested, and you still don't save yourself the effort of learning POSIX.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. How to extract a list of files in a specific directory?
    By Perspektyva in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2008, 02:02 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. searching files in directory
    By lobo in forum Windows Programming
    Replies: 5
    Last Post: 10-05-2001, 03:00 AM