При итерации вектора с файлами он выводит на экран каждое имя файла в нем, но когда я вызываю функцию readDocument метода, имя файла становится точкой. Я протестировал его вручную, установив имя файла, например: filename = "collection / file1.txt", он работает и читает файл.
Ниже приведен пример кода:
#include <iostream>
#include <vector>
#include <string>
#include <dirent.h>
#include <fstream>
void readDocument(string filePath){
string contents = "";
string line = "";
filePath = "collection/"+filePath;
ifstream fileReader(filePath);
// opening file
if(fileReader.is_open()){
while( getline(fileReader, line) ){
contents += line;
cout << contents << endl;
}
fileReader.close();
}
else{
cout << "Could not read file" << endl;
exit(0);
}
}
void readDirectory (vector<string> &files)
{
DIR *dir;
struct dirent *dp;
char cwd[1024];
getcwd(cwd, sizeof(cwd));
if((dir = opendir("collection")) == NULL) {
perror("\nUnable to open directory.");
exit(0);
}
while ((dp=readdir(dir)) != NULL) {
files.push_back(dp->d_name);
}
closedir(dir);
}
int main (){
vector<string> files;
readDirectory(files);
for(string _filename : files){
readDocument(_filename); /*Doesn't work*/
// cout << "File Name: "+_filename << endl; /*Works*/
}
}