背景信息:
我正在使用VS2013上的dirent.h (项目需求)在windows中构建一个文件树。我从here那里得到了dirent.h。我在运行时崩溃,从crt\crtw32\mbstowcs.c的第283行调试断言失败("retsize <= sizeInWords")。
为了测试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("."))失败,并显示上述调试断言
发布于 2014-12-11 02:37:47
在写这个问题的时候,我想出了答案:我忘了检查“。和"..“在Dirf的构造函数中(我记得在我的测试用例中这样做)。添加
while (d && d->d_name[0] == '.') { // skip '..' and '.'
d = readdir(dir);
}在dirent* d = readdir(dir)使错误消失之后。
https://stackoverflow.com/questions/27408409
复制相似问题