Thread: listing files in directories portably

  1. #1
    Registered User
    Join Date
    Nov 2004

    listing files in directories portably


    I'm writing a C program which performs operations on various files within directories. I'd like to be able to obtain a listing of files contained in a specific directory.

    The tricky bit is, I'm trying to make the code as portable as possible. I know that under DOS I can call a DOS-specific function, and that under UNIX I can parse the output of 'ls' (or other system calls).

    However, is there a better method than simply using preprocessor commands such as #define DOS 1 and then using #ifdef DOS ... #else ... #endif on huge blocks of code? It doesn't strike me as 'elegant' as I'd in effect be defining two different source codes and selecting the one to use at compile-time. Is there a better solution?


  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    There should be some compiler specific macros to tell you which Operating system you are using, I don't know them though. There really isn't a better way, if you read the linux kernel, they have a whole bunch of those ifdef's and stuff to test for architecture.
    Help populate a c/c++ help irc channel
    server: irc://
    channel: #c

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Well, since there is no standard file system, as far as C is concerned, why would their be a standard way to handle the file system?

    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  2. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  3. retrieving drive directories and files
    By PURE_POWER in forum A Brief History of
    Replies: 2
    Last Post: 06-01-2003, 02:01 PM
  4. Listing file in directories
    By Longie in forum C Programming
    Replies: 6
    Last Post: 09-26-2002, 08:56 AM
  5. displaying text files, wierd thing :(
    By Gades in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 05:18 PM