By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Dynamic String arrays

P: n/a
I need to call a function that returns the contents of a directory in a
string type array.

I can get the contents of the directory but I don't know how to deal with
the fact that I won't know the number of files in the directory ahead of
time.

What would be a good way to acomplish this?

Would it look something along the lines of:
foo(string& s[], int& count) ??

Thanks
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Suddn wrote:
I need to call a function that returns the contents of a directory in a
string type array.

I can get the contents of the directory but I don't know how to deal with
the fact that I won't know the number of files in the directory ahead of
time.

What would be a good way to acomplish this?

Would it look something along the lines of:
foo(string& s[], int& count) ??


You can try one of the standard containers :

bool GetDirectory( std::vector<std::string> & list );

or maybe if you're messing with the list

bool GetDirectory( std::list<std::string> & list );

Jul 19 '05 #2

P: n/a
In article <pa****************************@NoSpam.net>,
Suddn <Bo***@NoSpam.net> wrote:
I need to call a function that returns the contents of a directory in a
string type array.

I can get the contents of the directory but I don't know how to deal with
the fact that I won't know the number of files in the directory ahead of
time.

What would be a good way to acomplish this?
Use a vector to store the file names. You can add names to the vector in
such a way that the vector automatically expands as necessary:

#include <vector>
#include <string>

//....

vector<string> filenames;

while (there are more files in the directory)
{
string nextfile;
GetNextFile (nextfile);
filenames.push_back(nextfile);
}

Would it look something along the lines of:
foo(string& s[], int& count) ??


foo (vector<string>& filenames);

No need to return the count separately, because vectors keep track of how
big they are:

int numfiles = filenames.size();

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.