[英]opendir() throwing an instance of 'std::logic_error'
I have a path defined as std::string folder_path = "server/files/";
我有一个定义为std::string folder_path = "server/files/";
的路径. . I am trying to list the files in a directory.我正在尝试列出目录中的文件。 I am able to successfully open the file and list the files (see console output below);我能够成功打开文件并列出文件(参见下面的控制台 output); however, I get the following error after the files are listed:但是,列出文件后出现以下错误:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Aborted
Here is what i currently came up with.这是我目前想出的。 I read it is something to do with string not being NULL however folder_path will have a default value anyways so I am unsure how to proceed from here:我读到它与不是 NULL 的字符串有关,但是 folder_path 无论如何都会有一个默认值,所以我不确定如何从这里开始:
struct dirent *entry;
struct stat filestat;
DIR *dir = opendir(folder_path.c_str());
if(dir == NULL){
cout << "Not a valid directory: " << mount_path << endl;
}
if(errno == ENOTDIR){
cout << "OpenDir() Error (" << strerror(errno) << ")" << dir << endl;
}
cout << "Getting files from:" << mount_path << endl;
while((entry = readdir(dir))){
stat(entry->d_name, &filestat);
cout << entry->d_name << " " << ctime(&filestat.st_mtime);
}
closedir(dir);
output: output:
Getting files from: server/files/
. Sat Nov 16 06:54:14 2019
.. Sat Nov 16 03:21:09 2019
alpha.jpg Sat Nov 16 03:21:09 2019
stockphoto.jpg Sat Nov 16 03:21:09 2019
image_fj8u3sadu3.jpg Sat Nov 16 03:21:09 2019
image_ijdalksd213.jpg Sat Nov 16 03:21:09 2019
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Aborted
Code should run fine.代码应该运行良好。 Try this:尝试这个:
if (stat(entry->d_name, &filestat)){
cout << entry->d_name << " " << ctime(&filestat.st_mtime);
}else{ /* handle error here*/}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.