boost filesystem iterate

Started by
4 comments, last by multiversicationist 12 years, 5 months ago

copy(directory_iterator(ProjectDirectoryMain), directory_iterator(), ostream_iterator<directory_entry>(cout, "/n"));


This snippet goes through a given directory, and prints out the path and name of each file it holds. What I want to do instead of print, is to store in a variable, and also keep count.
Advertisement
You should be able to use a standard container to store the paths, and a regular for loop over the iterators. If you cannot do this, I think you're over-reaching your current knowledge by too large an amount. Writing a simple loop and using a simple container are far more basic than what you're trying to do now.

You should be able to use a standard container to store the paths, and a regular for loop over the iterators. If you cannot do this, I think you're over-reaching your current knowledge by too large an amount. Writing a simple loop and using a simple container are far more basic than what you're trying to do now.


Turns out all I had to do was this:


int size;
int loopcounter;
vector<directory_entry> files;
copy(directory_iterator(ProjectDirectoryMain), directory_iterator(), back_inserter(files));

size=files.size();
for (int loopcounter=0 ;loopcounter<size;++loopcounter) {cout << files.at(loopcounter);}


int size;
int loopcounter;
vector<directory_entry> files;
copy(directory_iterator(ProjectDirectoryMain), directory_iterator(), back_inserter(files));

size=files.size();
for (int loopcounter=0 ;loopcounter<size;++loopcounter) {cout << files.at(loopcounter);}


Better yet,

vector<directory_entry> files;
copy(directory_iterator(ProjectDirectoryMain), directory_iterator(), back_inserter(files));
for (auto file: files) cout << file << "\n";

Stephen M. Webb
Professional Free Software Developer

Just a quick note about your solution there...

You should either remove the 'int loopcounter' from the start of the code or remove the 'int' from the for loop. right now you're creating two variables with the same name. What's the value of loopcounter after the loop is done? ;)
Humble people don't refer to themselves as humble (W.R.T. "IMHO".)
You guys are great!

I'm still learning and I hate to ask stupid questions, but I learn a lot more in a lot less time when I do sometimes.

This topic is closed to new replies.

Advertisement