I am learning to use dirent.h. While the process is fascinating and interesting, I have ran into a problem with using d_name.
I want to do two things using d_name.
recursively search through the sub-directories. To this, when I encounter DT_DIR type files, I will make recursive call to the function
void iterateDirectory(char* filePath){ DIR* dirPtr; dirent* entry; entry = readdir(dirPtr); ... }
within itself using d_name of the directory as a new char* filePath parameter. So,
if(dirEntry->d_type == DT_DIR){ entry->d_name; iterateDirectory(entry->d_name); ... }
Open all files within a directory. To do this, when I encounter DT_REG files, I will create ifstream object and open the file using d_name. So,
if(dirEntry->d_type == DT_REG){ entry->d_name; ifstream fin(entry->d_name); if(fin.is_open) cout<<"Opened"<<endl; else cout<<"Not Opened"<<endl; ... }
The problem I am running into, is that neither the void iterateDirectory() function nor the ifstream fin() seems to recognize the entry->d_name as a valid input. When I call the iterate function using d_name or use ifstream with entry->d_name, my checks to see if the directory or file is open fails. The function itself is working, as I've checked the exact same function with different char* inputs. The only problem I can think of is that my function is not taking in the absolute path as the parameter.
My questions is how can I find the absolute path of a given file or sub-directory at the point of iteration. My initial solution was to make use of "." as that is the current directory. Store the address of "." into a string, and append "\"+entry->d_name. But I think the syntax is wrong.
Am I right about the absolute path problem? or is there another problem I am missing? If it is the absolute path problem, what is the syntax for getting the absolute path of a file?
P.S.
I've been informed in the past to minimize the amount of code I upload onto stack overflow for questions, and I presented what I figure to be the smallest required code. In case the information presented above is insufficient, I am linking github page for the code.
https://github.com/ForeverABoy/dirent.h_practice/blob/master/directoryIterator.cpp
Any and all helps are appreciated. Thank you!