C++ 目录列表程序未正确打开
以下是代码(适用于整个项目) 现在,我不知道为什么这不起作用,尽管我知道这不是因为开始时的“/” 如果有人能帮我诊断(至少)或解决这个问题,那就太好了。如果我遗漏了任何信息,请留下评论,我将进行编辑C++ 目录列表程序未正确打开,c++,linux,filesystems,dirent.h,C++,Linux,Filesystems,Dirent.h,以下是代码(适用于整个项目) 现在,我不知道为什么这不起作用,尽管我知道这不是因为开始时的“/” 如果有人能帮我诊断(至少)或解决这个问题,那就太好了。如果我遗漏了任何信息,请留下评论,我将进行编辑 编辑1:传递的参数是/和blarg(blarg,因为文件从未写入或打开)我将更改行: sprintf(buf, "%s/%s", dirName, ent->d_name); 到 这对我的测试产生了影响 而且 if (strcmp(tmppath, filename) =
编辑1:传递的参数是/和blarg(blarg,因为文件从未写入或打开)我将更改行:
sprintf(buf, "%s/%s", dirName, ent->d_name);
到
这对我的测试产生了影响
而且
if (strcmp(tmppath, filename) == 0)
continue;
将导致内存泄漏。我想把它改为:
if (strcmp(tmppath, filename) == 0)
{
free(tmppath);
continue;
}
我觉得其他一切都很好
更新
最好有一个directoryReader
的析构函数来关闭打开的目录
directoryReader::~directoryReader()
{
if (dir != NULL) closedir(dir);
}
此外,p_getfiles
可以重新组织,以便:
directoryReader
安装p\u getfiles
void p_getfiles_and_directories(char const* basepath,
char const* filename, //filename is to ignore
std::vector<std::string>& files,
std::vector<std::string>& directories)
{
// This function does not recurse directories.
// It just returns the file and sub-directories in the given
// basepath.
directoryReader dirr(basepath, S_IFREG | S_IFDIR);
char *tmppath = NULL;
struct stat st;
while ((tmppath = dirr.getNext()) != NULL)
{
if (strcmp(tmppath, filename) == 0)
continue;
if (goodpath(tmppath))
{
stat(tmppath, &st);
if (S_ISDIR(st.st_mode))
{
directories.push_back(tmppath);
}
else if (S_ISREG(st.st_mode))
{
files.push_back(tmppath);
}
}
free(tmppath);
}
}
void p_getfiles(char const* basepath, FILE *f, char const* filename) //filename is to ignore
{
// Get all the files and sub-directories in the given basepath.
std::vector<std::string> files;
std::vector<std::string> directories;
p_getfiles_and_directories(basepath, filename, files, directories);
// Recurse directories.
std::vector<std::string>::iterator iter = directories.begin();
std::vector<std::string>::iterator end = directories.end();
for ( ; iter != end; ++iter )
{
if (strcmp((*iter).c_str(), filename) == 0)
printf("uh oh...\n");
p_getfiles((*iter).c_str(), f, filename);
}
// Process files.
iter = files.begin();
end = files.end();
for ( ; iter != end; ++iter )
{
fprintf(stdout, "%s\n", (*iter).c_str());
}
}
void p_getfiles_和_目录(char const*basepath,
char const*filename,//要忽略的文件名
std::向量和文件,
std::向量和目录)
{
//此函数不递归目录。
//它只返回给定目录中的文件和子目录
//基本路径。
directoryReader-dirr(基本路径,S|u IFREG | S|u IFDIR);
char*tmppath=NULL;
结构统计;
while((tmppath=dirr.getNext())!=NULL)
{
if(strcmp(tmppath,filename)==0)
继续;
if(goodpath(tmppath))
{
统计局(tmppath和st);
中频(S_ISDIR(st.st_模式))
{
目录。推回(tmppath);
}
否则如果(S_ISREG(st.st_模式))
{
文件。推回(tmppath);
}
}
免费(tmppath);
}
}
void p_getfiles(char const*basepath,FILE*f,char const*filename)//文件名将被忽略
{
//获取给定basepath中的所有文件和子目录。
std::矢量文件;
std::向量目录;
p_getfiles_和_目录(基本路径、文件名、文件、目录);
//递归目录。
std::vector::iterator iter=目录。begin();
std::vector::iterator end=目录.end();
对于(;iter!=结束;++iter)
{
if(strcmp((*iter).c_str(),文件名)==0)
printf(“哦…\n”);
p_getfiles((*iter.c_str(),f,文件名);
}
//处理文件。
iter=files.begin();
end=files.end();
对于(;iter!=结束;++iter)
{
fprintf(stdout,“%s\n”,(*iter.c_str());
}
}
运行生成上述输出的程序时使用了哪些参数?我传递了/和blarg(因为它无论如何都不会写入文件)。(应该递归地列出系统上的所有文件)哪个更适合您?我仍然得到与以前完全相同的问题,只是现在它确实看起来有点像cleaner@phyrrus9是的,它做它应该做的。你在什么操作系统上运行它?你看到的错误可能是由权限问题引起的吗?我以root用户身份运行它。不过,由于这是为了在linux上运行,也许我应该在arch上试试。
if (strcmp(tmppath, filename) == 0)
{
free(tmppath);
continue;
}
directoryReader::~directoryReader()
{
if (dir != NULL) closedir(dir);
}
void p_getfiles_and_directories(char const* basepath,
char const* filename, //filename is to ignore
std::vector<std::string>& files,
std::vector<std::string>& directories)
{
// This function does not recurse directories.
// It just returns the file and sub-directories in the given
// basepath.
directoryReader dirr(basepath, S_IFREG | S_IFDIR);
char *tmppath = NULL;
struct stat st;
while ((tmppath = dirr.getNext()) != NULL)
{
if (strcmp(tmppath, filename) == 0)
continue;
if (goodpath(tmppath))
{
stat(tmppath, &st);
if (S_ISDIR(st.st_mode))
{
directories.push_back(tmppath);
}
else if (S_ISREG(st.st_mode))
{
files.push_back(tmppath);
}
}
free(tmppath);
}
}
void p_getfiles(char const* basepath, FILE *f, char const* filename) //filename is to ignore
{
// Get all the files and sub-directories in the given basepath.
std::vector<std::string> files;
std::vector<std::string> directories;
p_getfiles_and_directories(basepath, filename, files, directories);
// Recurse directories.
std::vector<std::string>::iterator iter = directories.begin();
std::vector<std::string>::iterator end = directories.end();
for ( ; iter != end; ++iter )
{
if (strcmp((*iter).c_str(), filename) == 0)
printf("uh oh...\n");
p_getfiles((*iter).c_str(), f, filename);
}
// Process files.
iter = files.begin();
end = files.end();
for ( ; iter != end; ++iter )
{
fprintf(stdout, "%s\n", (*iter).c_str());
}
}