How do you get a list of files currently in a directory??

Discussion in 'C++' started by Rob_S, Feb 28, 2006.

  1. Rob_S

    Rob_S Guest

    I have a program which saves time stamped files into time stamped
    directories.

    When I want to read these files, I get the current date and check for the
    existence of the directory using....

    while (!(_chdir(dirname)) == 0)
    {reduce the directory name (time) and check again}

    This method quickly finds the last directory but I don't know anything about
    the contents of the directory.

    I thought it would be easy to load the contents of this directory into a
    list which could be used to open files.
    I have been using dir command for years and took it for granted that there
    is a function to read directory contents but I can't seem to find any easy
    way to do this.

    How can directory contents be read?
    Is there a function or class?
    The only thing I can think of is to write the filenames to a database as
    they are being written and then check for their existence in a directory.

    Any ideas would be appreciated.
    Bob
    Rob_S, Feb 28, 2006
    #1
    1. Advertising

  2. Rob_S

    Rolf Magnus Guest

    Rob_S wrote:

    > I have a program which saves time stamped files into time stamped
    > directories.
    >
    > When I want to read these files, I get the current date and check for the
    > existence of the directory using....
    >
    > while (!(_chdir(dirname)) == 0)
    > {reduce the directory name (time) and check again}
    >
    > This method quickly finds the last directory but I don't know anything
    > about the contents of the directory.
    >
    > I thought it would be easy to load the contents of this directory into a
    > list which could be used to open files.
    > I have been using dir command for years and took it for granted that there
    > is a function to read directory contents but I can't seem to find any easy
    > way to do this.
    >
    > How can directory contents be read?
    > Is there a function or class?


    Not in standard C++. The _chdir() function you're using above isn't a
    standard C++ function either. So you have to resort to system-specific
    functions (For a POSIX compliant system, they would e.g. be opendir(),
    readdir(), closedir()), which are best discussed in a newsgroup about the
    operating system/compiler/library you are using.
    Rolf Magnus, Feb 28, 2006
    #2
    1. Advertising

  3. Rob_S

    Guest

    Rolf Magnus wrote:
    > Rob_S wrote:


    > > How can directory contents be read?
    > > Is there a function or class?

    >
    > Not in standard C++. The _chdir() function you're using above isn't a
    > standard C++ function either. So you have to resort to system-specific
    > functions


    Or a portable library, like boost::filesystem. (Which is mostly
    off-topic here,
    but www.boost.org has enough documentation).
    See also the FAQ about the standard library and non-standard libraries.

    HTH,
    Michiel Salters
    , Mar 1, 2006
    #3
    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. perseus
    Replies:
    2
    Views:
    372
    Noah Roberts
    Aug 13, 2003
  2. Replies:
    4
    Views:
    2,121
  3. Deep_Feelings
    Replies:
    12
    Views:
    480
    Paul Moore
    Jul 15, 2009
  4. Kenneth McDonald
    Replies:
    1
    Views:
    94
    Brian Candler
    Oct 27, 2008
  5. iMath
    Replies:
    8
    Views:
    278
    emile
    Nov 13, 2012
Loading...

Share This Page