Я пытаюсь прочитать все txt-файлы из папки, включая файлы txt из подкаталогов выбранной папки, используя C++.Как читать все txt-файлы из папки? (включая подпапки)
Я внедрил версию программы, которая читает все текстовые файлы из определенной папки, но не выполняет итерацию ко всем подпапкам.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iterator>
#include <string>
#include <dirent.h>
using namespace std;
int main() {
DIR* dir;
dirent* pdir;
dir = opendir("D:/"); // open current directory
int number_of_words=0;
int text_length = 30;
char filename[300];
while (pdir = readdir(dir))
{
cout << pdir->d_name << endl;
strcpy(filename, "D:/...");
strcat(filename, pdir->d_name);
ifstream file(filename);
std::istream_iterator<std::string> beg(file), end;
number_of_words = distance(beg,end);
cout<<"Number of words in file: "<<number_of_words<<endl;
ifstream files(filename);
char output[30];
if (file.is_open())
{
while (!files.eof())
{
files >> output;
cout<<output<<endl;
}
}
file.close();
}
closedir(dir);
return 0;
}
Что следует изменить для этой программы для поиска txt-файлов в подпапках выбранной папки?
Посмотрите на [Boost, файловая система] (http://www.boost.org/doc/libs/1_56_0/libs/filesystem/doc/tutorial. HTML) –