【问题标题】:"retsize <= sizeInWords" in mbstowcs in dirent.h on WindowsWindows 上 dirent.h 中 mbstowcs 中的“retsize <= sizeInWords”
【发布时间】:2014-12-10 18:37:46
【问题描述】:

背景资料:

我在 VS2013 上使用 dirent.h(项目要求)在 windows 中构建文件树。我从here 得到了dirent.h。我在运行时因 crt\crtw32\mbstowcs.c 第 283 行的调试断言失败(“retsize

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

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(".", ""))

所以要构建我的树,我有:

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(".")) 失败并出现上述调试断言

【问题讨论】:

    标签: c++ dirent.h


    【解决方案1】:

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

    while (d && d->d_name[0] == '.') { // skip '..' and '.'
        d = readdir(dir);
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 2016-12-25
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 2019-04-13
      • 2015-05-05
      相关资源
      最近更新 更多