0

I am writing a function that will take in a text file and do some manipulation on it. The files are stored in somewhat different places, such as: /Documents/news/sports/something.txt where sports will be a folder with 20+ txt files

There are 20 more categories, stored such as: /Documents/news/next_category/something_else.txt

So the problem is that I want to pass in every txt file inside each folder inside the news folder, into my program, one by one. Is there a way to do this?

Thanks

rkeeey
  • 15
  • 2

2 Answers2

3

i think you want to recursively enumerate the contents of a directory. I would use boost filesystem. It even has a recursive dir walk sample http://www.boost.org/doc/libs/1_61_0/libs/filesystem/doc/index.htm

also look here for other solutions How do you iterate through every file/directory recursively in standard C++?

Community
  • 1
  • 1
pm100
  • 48,078
  • 23
  • 82
  • 145
0

Not with pure C++. C++ has no concept of directories, so you'll either have to interface with your OS's directory functions (like the C POSIX opendir and family) or use some library (like boost::filesystem)

Taywee
  • 1,313
  • 11
  • 17
  • its interesting that c++ has no concept of directories but it does understand threads - an arguably more complex concept – pm100 Jun 03 '16 at 21:39
  • I agree that it is weird, but threads were a more recent addition themselves. I think c++ filesystem stuff is supposed to be likely in c++17. – Taywee Jun 03 '16 at 21:50
  • @pm100 Generalizing directories across all possible operating systems is not as trivial as you might think. – Barmar Jun 03 '16 at 22:56
  • @Barmar Neither is generalizing threads. – Taywee Jun 03 '16 at 23:08