Dynamic String arrays

S

Suddn

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
 
G

Gianni Mariani

Suddn said:
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 );
 
J

Jon Bell

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();
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top