I may be way off base with a few things I say, that is not intentional. The area that I am venturing in to with this is completely new for me.
I have dirent.h added as a header
I have created the following:
CString tempString;
DIR *dir;
If I am not mistaken, dirent.h has to do with directory structures.
DIR *dir is what I am trying to assign a directory structure to. For example “C:\users\user\MyDocuments”
tempString is a CString that currently has the directory that the user chooses stored in it. I am trying to get the value of tempString and assign it to dir.
What I am trying to do as a whole is make a list of all the files and folders in a directory. All the research I have done so far points to using dirent.h to do this. This is a file parsing app I am making. I have done work before like this with directories where I already knew all the names of the files and folders in advance but never where I will not have that information and need my app to look at the folder and gather what is in it on its own.
Correct, I came to that conclusion a while back today. The problem is that it is not working and I can not see why. I KNOW that this will be something small and simple. Here is my code:
Look for this line:
"DIR *dir = opendir(Var_Dir::GetBuffer()); //Errors out no matter what I try here. I know it is something simpe"
////////////////////
////////////////////
//And here is where I am stuck. I know this line is not right
DIR *dir = opendir(Var_Dir::GetBuffer()); //Errors out no matter what I try here. I know it is something simpe
////////////////////
////////////////////
struct dirent *ent;
//CString csTemp;
if (dir != NULL)
{
AfxMessageBox(_T("Made it here.")); //For testing purposes. This confirms that dir received the value.
while ((ent = readdir(dir)) != NULL)
{
fileOUT << ent << "\n";
printf("%s\n", ent->d_name);
}
}
else
{
AfxMessageBox(_T("Not working.")); //For testing purposes.
}
A few months ago when I went looking for "dirent.h" I found these two sites. I do not remember which site I downloaded from first, but "GitHub" looks most familiar, but the readme.md link will direct you to "softagalleria.net".
With what I found this is the code I put together. It is condensed down from what I used, but I think it should work, speaking more main than the function. The only parts of the function I changed was to comment out what I did not need.
Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <dirent.h>
#define PATH "..\\..\\" // Or any path you need.
int main()
{
char buff[MAX_PATH]{ '\0' }; // For ShowDir()
const std::string fileType{ ".cpp" }; // File Type to List.
constchar *directory{ PATH }; // Directory to List.
list_directory(directory, fileType);
// You may want a pause here.
return 0;
}