C++MFC正在从编辑框中读取Cstring以进行文件读取(std::filesystem异常未处理的内存问题)



我的代码要实现的是在给定文件路径输入和输出后读取文件名(开关1:同一文件夹下的文件((开关2:目录下的所有文件名,包括子目录(,并且不将文件夹名存储到矢量中(1.png 2.png 3.png,但不是ChildFolder(。

如果您需要有关文件夹结构的额外信息,请参阅我之前的问题

#define IDC_RADIO1                      1000
#define IDC_RADIO2                      1001
#define IDC_EDIT1                       1002
#define IDC_BUTTON1                     1003
#define IDC_LIST1                       1004

这个窗口应用程序有2个单选框,1个编辑框供用户输入路径名,1个按钮用于启动整个过程,1个列表框用于显示读取的文件

主要功能如下:

void CMFCApplication3Dlg::OnBnClickedButton1()
{
string a;
vector<string> caseOne,
caseTwo;

代码的最后两行是我遇到问题的地方

a=";C: \用户\用户。。。代码有效,但输出似乎有数组元素逻辑问题,列表框中的输出为Nothing,1.png,2.png,而不是1.png、2.png编辑注意:这部分现在有效

代码directory_迭代器列表(str(似乎有内存问题,原因是
a=enterPath.GetBuffer((我不知道该怎么办。

ErrorMessageMFCApplication3.exe中0x7525B502处未处理的异常:内存位置0x0133E438处的Microsoft C++异常:std::files系统::filesystem_error。

屏幕截图位于下方

理想情况下,我将删除一个="0";C: \用户\用户。。。并使用a=enterP。。如果它工作

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
}

编辑:

这就是我想要实现的(UI屏幕截图(

我为用户预先确定文件夹路径的工作版本

这就是我希望用户输入的

错误消息我得到

文件夹内容看起来像

父文件夹的子文件夹

按钮1 的完整代码

我还发现了一个我以前没有注意到的新错误,输出是png.1 png.2而不是1.png2.png,这不应该发生,因为我在使用cout<lt;;一段时间后的工作版本,我为用户预先确定文件夹路径

经过一番研究,我更改了代码,最终使其能够在中工作

//Answer
string a;
CString stri;//Read text from edit control box and convert it to std::string
GetDlgItem(IDC_EDIT1)->GetWindowText(stri);
a = CT2A(stri);
//This code is something I also tried but the string a it passed back is ""
/*CString b;
b = enterPath;//a control variable I added from IDC_EDIT1
a = CT2A(b);*/

关于文件名1.find("."(<filename1.length((它起作用,因为find((会传回查找字符的第一个位置

所以<filename1.length((表示find((的返回不能长于其自身的字符串,当未找到任何内容时,返回-1if语句仍然运行,但不向AddString传递任何内容。(这就是为什么它感觉像是创可贴修复(

因此,编写if语句的更好方法是

if(filename1.find(".") != -1)
//or
if(filename1.find(".") != string::npos)//npos is -1

由于我找到了一种更好的方法来区分#include文件系统中的文件名和文件夹,我最终删除了上面的代码

因为出于某种原因,我认为文件夹名称不能有""其中

//code I switched to 
if(is_regular_file(it))

最新更新