简体   繁体   English

Windows上dirent.h中mbstowcs中的“ retsize <= sizeInWords”

[英]“retsize <= sizeInWords” in mbstowcs in dirent.h on Windows

Background information: 背景资料:

I'm building a file tree in windows using dirent.h (project requirement) on VS2013. 我正在VS2013上使用dirent.h(项目要求)在Windows中构建文件树。 I got dirent.h from here . 我从这里得到了dirent.h。 I crash at runtime with a debug assertion failed ("retsize <= sizeInWords") from crt\\crtw32\\mbstowcs.c, line 283. 我在运行时崩溃,原因是crt \\ crtw32 \\ mbstowcs.c,第283行的调试断言失败(“ retsize <= sizeInWords”)。

To test dirent.h, I used 为了测试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 + "  ");
      }
    }
  }
}

which worked (called from main as printDir(".", "")) 起作用的(从main调用为printDir(“。”,“”))

so to build my tree I have: 所以要建立我的树,我有:

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 = "");

However, calling Dirf(opendir(".")) fails with the debug assertion described above 但是,调用Dirf(opendir(“。”))失败,发生上述调试断言

While composing the question, I figured out the answer: I forgot to check for "." 在撰写问题时,我想出了答案:我忘了检查“”。 and ".." in Dirf's constructor (I remembered to do it in my test case). 和Dirf的构造函数中的“ ..”(我记得在我的测​​试用例中这样做)。 Adding 添加

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

after dirent* d = readdir(dir) made the error go away. dirent* d = readdir(dir)使错误消失之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM