create an array of strings that hold filenames

This is a discussion on create an array of strings that hold filenames within the C++ Programming forums, part of the General Programming Boards category; Given a string that holds a the path to a directory, is there a way to create an array of ...

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

    create an array of strings that hold filenames

    Given a string that holds a the path to a directory, is there a way to create an array of strings (or char* or whatever) where each slot in the array holds the name of a file in that directory? I've actually done this in Perl, which is incredibly simple, but I have no experience with dealing with files in this way with C++. Any help would be greatly appreciated.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    std::vector<std::string> filename;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    amendment

    ok... i guess i didn't quite phrase my question right. i dont know how to go about getting each filename to then store into the vector, if all i have is the directory name.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    That's okay, I type slow. This is based on what is in the FAQ.
    Code:
    //
    // Base: http://faq.cprogramming.com/cgi-bin/...&id=1044780608
    //
    #include <iostream> 
    #include <string> 
    #include <vector> 
    #include <algorithm> 
    #include <iterator> 
    #include <dirent.h> 
    
    int main(void)
    {
       std::vector<std::string> filename;
       DIR *d = opendir(".");
       if ( d )
       {
          struct dirent *dir;
          // Load vector with filenames.
          while ( (dir = readdir(d)) != NULL )
          {
             filename.push_back(dir->d_name);
          }
          closedir(d);
       }
       // Show contents of vector.
       std::copy(filename.begin(), filename.end(),
                 std::ostream_iterator<std::string> (std::cout, "\n"));
       return 0;
    }
    If it doesn't work for you, you may want to visit the FAQ to find one that is more likely to work.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    finkus meet Windows Programming
    Windows Programming meet finkus

    Directory Logging

    EDIT: Sinkula's method is also very good and probably more portable.
    Last edited by SlyMaelstrom; 12-13-2005 at 09:17 PM.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    ah... right, the FAQ. i probably should've looked there first, huh? anyway, thanks for the help Dave_Sinkula. i'll check that out now.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    haha... thanks SlyMaelstrom... it's a much overdue introduction.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Ah yeah, I see from your other posts you've met.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner: dynamic array of strings
    By pc2-brazil in forum C++ Programming
    Replies: 10
    Last Post: 04-29-2008, 05:29 PM
  2. remove strings from array
    By ipe in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 04:53 AM
  3. Replies: 5
    Last Post: 11-20-2001, 12:48 PM
  4. Replies: 4
    Last Post: 11-07-2001, 02:46 PM
  5. array of strings + more
    By null in forum C Programming
    Replies: 10
    Last Post: 10-01-2001, 04:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21