What my code is suppose to achieve is to read in the file names after given a file path input and output (switch 1: the files under the same folder)(switch 2: all the file names under the directory including sub-directories), and to not store the folder names into vector (1.png 2.png 3.png but not ChildFolder).
If you need extra information regarding the folder structures it is provided in my previous question.
#define IDC_RADIO1 1000
#define IDC_RADIO2 1001
#define IDC_EDIT1 1002
#define IDC_BUTTON1 1003
#define IDC_LIST1 1004
This window app have 2 radiobox, 1 editbox for user to input path name, 1 button to start the whole process, and 1 listbox to display the files read
Main function starts below:
void CMFCApplication3Dlg::OnBnClickedButton1()
{
string a;
vector<string> caseOne,
caseTwo;
The bottom two lines of code is where I have issues
The a = "C:\Users\User... code works but the output seems to have a array element logic problem going on where the output in the listbox is Nothing,1.png,2.png instead of 1.png,2.png Edit Note :this part works now
The code directory_iterator list(str); seems to have a memory problem caused by
a = enterPath.GetBuffer(); and I don't know what to do to fix it.
ErrorMessage Unhandled exception at 0x7525B502 in MFCApplication3.exe: Microsoft C++ exception: std::filesystem::filesystem_error at memory location 0x0133E438.
The screen shot is provided below
I would ideally delete a = "C:\Users\User... and use a = enterP.. if it works
a = "C:\\Users\\User\\Desktop\\ParentFolder";//This line is used to test code, will move on to having user input to IDC_EDIT1
//a = enterPath.GetBuffer();//Code I want to use
path str = a;
directory_iterator list(str);
CListBox* listBox = (CListBox*)GetDlgItem(IDC_LIST1);
UpdateData(TRUE);//update the data
int checkRadio = GetCheckedRadioButton(IDC_RADIO1, IDC_RADIO2);
switch (checkRadio)
{
case IDC_RADIO1:
list = directory_iterator(a); //reset directory_iterator evertime button clicked to read new file
for (auto& it : list) { //go through the directory_iterator list
string filename1 = it.path().filename().string(); //get file names then convert filename into string format
if (filename1.find(".") < filename1.length()/*or != string::npos*/ ) { // find file name to rule out folders cause files contain '.'
caseOne.push_back(filename1); //putting the filenames into the vector
CString fileunderPath; //bottom 3 lines of code convert string to CString
fileunderPath = filename1.c_str();
listBox->AddString(fileunderPath);
}
}
break;
//same problem that should be resolved if case 1 is fixed
case IDC_RADIO2://NOTE: NEED TO ADD CLEAR LISTBOX FUNCTION and vector
list = directory_iterator(a);
for (auto& dirEntry : recursive_directory_iterator(a)) {
string filename2 = dirEntry.path().filename().string();
if (filename2.find(".") != string::npos) { // find file name to rule out folders cause files contain '.'
caseTwo.push_back(filename2);
}
}
break;
}
UpdateData(FALSE);//finish updating the data
}
Edit:
This is what I want to achieve(UI screenshot)
Working version where I predetermine the folder path for user
This is what I want user to enter
What the folder content looks like
A child folder of the parent folder
I also found a new bug I didn't notice before,The output is png.1 png.2 instead of 1.png 2.png, this should not have happened since I made it work without problem when I used cout<<; a while back Working version where I predetermine the folder path for user