简体   繁体   English

opendir() 抛出 'std::logic_error' 的实例

[英]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.

相关问题 为什么有 std::logic_error? - Why is there std::logic_error? 抛出&#39;std :: logic_error&#39;what()实例后调用终止终止:basic_string :: __ S_construct null无效 - terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid 如何解决在引发&#39;std :: logic_error&#39;what()实例之后调用的终止方法what():basic_string :: __ M_construct null无效 - how to fix terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid 在抛出“std::logic_error”实例后调用终止 what(): basic_string::_M_construct null 无效(帮助) - terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid (HELP) 抛出&#39;std :: logic_error&#39;what()实例后调用终止终止:basic_string :: __ M_construct null无效 - terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid std :: logic_error :: logic_error未定义 - std::logic_error::logic_error undefined `std::logic_error::logic_error(std::logic_error const&) 的多重定义 - multiple definition of `std::logic_error::logic_error(std::logic_error const&) 如何避免错误:在抛出&#39;std :: logic_error&#39;的实例后调用terminate what():basic_string :: _ S_construct null无效 - How to avoid the error: terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid 我应该抓住std :: logic_error吗? - Should i catch std::logic_error? std :: logic_error而不是return false - std::logic_error instead of return false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM