首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"retsize <= sizeInWords“in mbstowcs in dirent.h

"retsize <= sizeInWords“in mbstowcs in dirent.h
EN

Stack Overflow用户
提问于 2014-12-11 02:37:47
回答 1查看 322关注 0票数 0

背景信息:

我正在使用VS2013上的dirent.h (项目需求)在windows中构建一个文件树。我从here那里得到了dirent.h。我在运行时崩溃,从crt\crtw32\mbstowcs.c的第283行调试断言失败("retsize <= sizeInWords")。

为了测试dirent.h,我使用了

代码语言:javascript
复制
void printDir(std::string dir, std::string tab)
{
  DIR* d = opendir(dir.c_str());
  dirent* ent = nullptr;
  while (ent = readdir(d)) {
    if (ent->d_name[0] != '.') {
      std::cout << tab << ent->d_name << std::endl;
      if (ent->d_type == DT_DIR) {
        printDir(dir + "/" + ent->d_name, tab + "  ");
      }
    }
  }
}

它起作用了(从main调用为printDir(".",""))

因此,要构建我的树,我需要:

代码语言:javascript
复制
struct Dirf {
  std::string getFullPath() {
    if (out) {
      return out->getFullPath() + "/" + ent.d_name;
    }
    else return ent.d_name;
  }

  Dirf(DIR* dir, Dirf* parent = nullptr)
    : out(parent)
  {
    if (dir) {
      dirent* d = readdir(dir);
      if (d) {
        ent = *d;
        if (dir) {
          next = std::make_shared<Dirf>(dir, parent);
        }
        if (ent.d_type == DT_DIR) {
          DIR* inDir = opendir(getFullPath().c_str());
          in = std::make_shared<Dirf>(inDir, this);
          closedir(inDir);
        }
      }
    }
  }

private:
  typedef std::shared_ptr<Dirf> point;
  friend  std::string to_string(Dirf, std::string);

  dirent ent;
  Dirf* out; // parent to this; in->out == this, out->in == this;
  point in, // in != null iff car.d_type == DT_DIR
        next; // next entry in the dir
};

std::string to_string(Dirf, std::string tab = "");

但是,调用Dirf(opendir("."))失败,并显示上述调试断言

EN

回答 1

Stack Overflow用户

发布于 2014-12-11 02:37:47

在写这个问题的时候,我想出了答案:我忘了检查“。和"..“在Dirf的构造函数中(我记得在我的测试用例中这样做)。添加

代码语言:javascript
复制
while (d && d->d_name[0] == '.') { // skip '..' and '.'
    d = readdir(dir);
}

dirent* d = readdir(dir)使错误消失之后。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27408409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档