List files in current directory

Discussion in 'C++' started by Kristan, Oct 6, 2006.

  1. Kristan

    Kristan Guest

    Hi there, quick question, how would I retrieve a list of files in ANSI C in
    a purely platform independent way?

    Any pointers would be great!

    thanks
    Kristan
     
    Kristan, Oct 6, 2006
    #1
    1. Advertising

  2. "Kristan" <> wrote in message
    news:452624ee$0$16554$...
    > Hi there, quick question, how would I retrieve a list of files in ANSI C

    in
    > a purely platform independent way?
    >
    > Any pointers would be great!
    >

    (I'm posting after reading on comp.lang.c. I don't know if it's suited to
    the other NG's you posted to and you didn't set followups to the NG you are
    reading from. Since, I don't know from which NG you are reading replies,
    all NG's you posted to whether appropriate or not get this message.)

    "in ANSI C"
    - You don't. You could use POSIX C routines.
    "in a purely platform independent way"
    - You might use a platform specific version of Doug Gwyn's Public Domain
    libndir package or create a multiplatform version from the various versions.
    for BSD, libndir.tar.Z http://ftp.br.xemacs.org/pub/unix-c/languages/c/
    for POSIX, libndir-posix.tar.Z
    http://ftp.br.xemacs.org/pub/unix-c/languages/c/
    for DOS, (several versions exist, I'm not looking them up unless you
    _actually_ need them, i.e., beg)
    - You might look at how multi-platform applications which store directory
    structures work, like Info-ZIP, PDTar (Public Domain tar), GNU tar, etc...
    Infozip http://www.info-zip.org/pub/infozip/
    pdtar.tar.Z http://ftp.br.xemacs.org/pub/unix-c/tapes/

    Programs like Info-ZIP and PDTar have to create their routines which perform
    directory access identically on many platforms. However, those routines may
    not be integrated into a single file...


    Rod Pemberton
     
    Rod Pemberton, Oct 6, 2006
    #2
    1. Advertising

  3. Kristan

    Kristan Guest

    Hi there, well, POSIX sounds promising, as it seems to be supported on Linux
    and Windows (2000 upwards), basically I'm using Windows for development and
    the Debian Linux server is the platform.

    Do you know whether POSIX is available with my Visual Studio 2005 C
    compiler? Or would I have to obtain it separately?

    thanks
    Kristan


    "Rod Pemberton" <> wrote in message
    news:eg5ako$s9o$...
    >
    > "Kristan" <> wrote in message
    > news:452624ee$0$16554$...
    >> Hi there, quick question, how would I retrieve a list of files in ANSI C

    > in
    >> a purely platform independent way?
    >>
    >> Any pointers would be great!
    >>

    > (I'm posting after reading on comp.lang.c. I don't know if it's suited to
    > the other NG's you posted to and you didn't set followups to the NG you
    > are
    > reading from. Since, I don't know from which NG you are reading replies,
    > all NG's you posted to whether appropriate or not get this message.)
    >
    > "in ANSI C"
    > - You don't. You could use POSIX C routines.
    > "in a purely platform independent way"
    > - You might use a platform specific version of Doug Gwyn's Public Domain
    > libndir package or create a multiplatform version from the various
    > versions.
    > for BSD, libndir.tar.Z http://ftp.br.xemacs.org/pub/unix-c/languages/c/
    > for POSIX, libndir-posix.tar.Z
    > http://ftp.br.xemacs.org/pub/unix-c/languages/c/
    > for DOS, (several versions exist, I'm not looking them up unless you
    > _actually_ need them, i.e., beg)
    > - You might look at how multi-platform applications which store directory
    > structures work, like Info-ZIP, PDTar (Public Domain tar), GNU tar, etc...
    > Infozip http://www.info-zip.org/pub/infozip/
    > pdtar.tar.Z http://ftp.br.xemacs.org/pub/unix-c/tapes/
    >
    > Programs like Info-ZIP and PDTar have to create their routines which
    > perform
    > directory access identically on many platforms. However, those routines
    > may
    > not be integrated into a single file...
    >
    >
    > Rod Pemberton
    >
    >
     
    Kristan, Oct 6, 2006
    #3
  4. Kristan

    Simon Biber Guest

    Kristan wrote:
    > Hi there, well, POSIX sounds promising, as it seems to be supported on Linux
    > and Windows (2000 upwards), basically I'm using Windows for development and
    > the Debian Linux server is the platform.
    >
    > Do you know whether POSIX is available with my Visual Studio 2005 C
    > compiler? Or would I have to obtain it separately?


    I don't think it's directly supported. There is a POSIX subsystem for
    Windows that you can download, but it does not use the Visual Studio C
    compiler.

    My approach when dealing with platform-specifics is to abstract the
    functionality into functions that are conditionally compiled on each system.

    Below is a directory lister for Windows, Unix/POSIX and MS-DOS.

    #include <stdio.h>

    #ifdef _WIN32

    /* Compiling for Windows */

    #include <windows.h>

    int main(void)
    {
    WIN32_FIND_DATA f;
    HANDLE h = FindFirstFile("./*", &f);
    if(h != INVALID_HANDLE_VALUE)
    {
    do
    {
    puts(f.cFileName);
    } while(FindNextFile(h, &f));
    }
    else
    {
    fprintf(stderr, "Error opening directory\n");
    }
    return 0;
    }

    #else
    #ifdef __unix__

    /* Compiling for UNIX / POSIX */

    #include <sys/types.h>
    #include <dirent.h>

    int main(void)
    {
    DIR *dir = opendir(".");
    if(dir)
    {
    struct dirent *ent;
    while((ent = readdir(dir)) != NULL)
    {
    puts(ent->d_name);
    }
    }
    else
    {
    fprintf(stderr, "Error opening directory\n");
    }
    return 0;
    }

    #else
    #ifdef __TURBOC__

    /* Compiling for MS-DOS */

    #include <dir.h>

    int main(void)
    {
    struct ffblk ffblk;
    if(findfirst("*.*", &ffblk, 0) == 0)
    {
    do
    {
    puts(ffblk.ff_name);
    } while(findnext(&ffblk) == 0);
    }
    else
    {
    fprintf(stderr, "Error opening directory\n");
    }
    return 0;
    }

    #else
    #error Unsupported Implementation
    #endif
    #endif
    #endif
     
    Simon Biber, Oct 6, 2006
    #4
  5. >Hi there, quick question, how would I retrieve a list of files in ANSI C in
    >a purely platform independent way?


    1. Prompt the user for the file names, one at a time.
    2. Get the list of file names from argv[].
    3. Open a file containing a list of file names and read it, one line at a time.
     
    Gordon Burditt, Oct 7, 2006
    #5
  6. Kristan

    Joe Wright Guest

    Kristan wrote:
    > Hi there, quick question, how would I retrieve a list of files in ANSI C in
    > a purely platform independent way?
    >
    > Any pointers would be great!
    >
    > thanks
    > Kristan
    >
    >

    Windows:
    system("dir /b > file.lst");
    Linux:
    system("ls > file.lst");

    might do it.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Oct 7, 2006
    #6
  7. Joe Wright <> writes:
    > Kristan wrote:
    >> Hi there, quick question, how would I retrieve a list of files in
    >> ANSI C in a purely platform independent way?
    >> Any pointers would be great!
    >> thanks
    >> Kristan

    > Windows:
    > system("dir /b > file.lst");
    > Linux:
    > system("ls > file.lst");
    >
    > might do it.


    Or it might not.

    <OT>
    In both cases, the command applies only to the current directory,
    whatever that happens to be. The Unix command skips any files whose
    names start with '.'. The order in which the files are listed may or
    may not depend on the current locale. Either command will fail if you
    don't have write permission in the current directory. If "file.lst"
    already exists, the command will either clobber it or fail; if it
    doesn't exist, you've just added a new file that may or may not show
    up in the listing.
    </OT>

    Since you've provided different solutions for Windows and Linux, it's
    obviously not "purely platform independent", which is what the OP was
    asking for. The fact that you're using the standard function system()
    doesn't make the code platform independent; it merely delays any
    failure until execution time.

    There is no purely platform independent solution.

    Both Windows and Linux provide system-specific mechanisms for
    retrieving a list of files (the Linux solution should work on any
    Unix-like system). These mechanisms are far more flexible, and they
    don't depend on creating and reading a temporary file in the very
    directory you're trying to examine

    This is one of those cases where trying to write portable code is a
    waste of time; the non-portable solutions work better, and the
    seemingly portable solution isn't portable at all.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 7, 2006
    #7
  8. Kristan

    Ben Voigt Guest

    "Kristan" <> wrote in message
    news:452632e7$0$24500$...
    > Hi there, well, POSIX sounds promising, as it seems to be supported on
    > Linux and Windows (2000 upwards), basically I'm using Windows for
    > development and the Debian Linux server is the platform.
    >
    > Do you know whether POSIX is available with my Visual Studio 2005 C
    > compiler? Or would I have to obtain it separately?


    For your specific need, look at:
    http://www.two-sdg.demon.co.uk/curbralan/code/dirent/dirent.html

    >
    > thanks
    > Kristan
    >
    >
    > "Rod Pemberton" <> wrote in message
    > news:eg5ako$s9o$...
    >>
    >> "Kristan" <> wrote in message
    >> news:452624ee$0$16554$...
    >>> Hi there, quick question, how would I retrieve a list of files in ANSI C

    >> in
    >>> a purely platform independent way?
    >>>
    >>> Any pointers would be great!
    >>>

    >> (I'm posting after reading on comp.lang.c. I don't know if it's suited
    >> to
    >> the other NG's you posted to and you didn't set followups to the NG you
    >> are
    >> reading from. Since, I don't know from which NG you are reading replies,
    >> all NG's you posted to whether appropriate or not get this message.)
    >>
    >> "in ANSI C"
    >> - You don't. You could use POSIX C routines.
    >> "in a purely platform independent way"
    >> - You might use a platform specific version of Doug Gwyn's Public Domain
    >> libndir package or create a multiplatform version from the various
    >> versions.
    >> for BSD, libndir.tar.Z http://ftp.br.xemacs.org/pub/unix-c/languages/c/
    >> for POSIX, libndir-posix.tar.Z
    >> http://ftp.br.xemacs.org/pub/unix-c/languages/c/
    >> for DOS, (several versions exist, I'm not looking them up unless you
    >> _actually_ need them, i.e., beg)
    >> - You might look at how multi-platform applications which store directory
    >> structures work, like Info-ZIP, PDTar (Public Domain tar), GNU tar,
    >> etc...
    >> Infozip http://www.info-zip.org/pub/infozip/
    >> pdtar.tar.Z http://ftp.br.xemacs.org/pub/unix-c/tapes/
    >>
    >> Programs like Info-ZIP and PDTar have to create their routines which
    >> perform
    >> directory access identically on many platforms. However, those routines
    >> may
    >> not be integrated into a single file...
    >>
    >>
    >> Rod Pemberton
    >>
    >>

    >
    >
     
    Ben Voigt, Oct 10, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TJS
    Replies:
    1
    Views:
    546
    Eliyahu Goldin
    Jun 23, 2004
  2. Kristan

    List files in current directory

    Kristan, Oct 6, 2006, in forum: C Programming
    Replies:
    10
    Views:
    834
    Ben Voigt
    Oct 10, 2006
  3. Marc Heiler
    Replies:
    4
    Views:
    114
    ara.t.howard
    Nov 18, 2007
  4. Adam Petrie
    Replies:
    8
    Views:
    333
    Adam Petrie
    Oct 11, 2004
  5. iMath
    Replies:
    8
    Views:
    317
    emile
    Nov 13, 2012
Loading...

Share This Page