- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 dirent.h
中提供的功能递归打开文件我的问题是:我无法跳过无法打开的目录。我希望它打开它可以打开的目录并跳过它不能打开的目录并移动到下一个目录而不是失败退出。我应该怎么做才能解决这个问题?
这是我尝试使用的一个简单代码
int acessdirs(const char *path)
{
struct dirent *entry;
DIR *dp;
char fpath[300];
if(dp=opendir(path))
{
while((entry=readdir(dp)))
do things here
}
else
{
std::cout<<"error opening directory";
return 0;
}
return 1;
}
我在 windows 7
上使用了相同的样式并且它工作正常。但是它在 windows xp
上崩溃了,当我调试它时我发现它在尝试打开时崩溃了“系统卷信息”
。我真的不需要访问这个文件夹,我希望有什么方法可以跳过它。
这是我的真实代码:
有点长。
int listdir(const char *path)
{
struct dirent *entry;
DIR *dp;
if(dp = opendir(path))
{
struct stat buf ;
while((entry = readdir(dp)))
{
std::string p(path);
p += "\\";
p += entry->d_name;
char fpath[300];
if(!stat(p.c_str(), &buf))
{
if(S_ISREG(buf.st_mode))
{
sprintf(fpath,"%s\\%s",path,entry->d_name);
stat(fpath, &buf);
std::cout<<"\n Size of \t"<<fpath<<"\t"<<buf.st_size;
fmd5=MDFile (fpath);
}//inner second if
if(S_ISDIR(buf.st_mode) &&
// the following is to ensure we do not dive into directories "." and ".."
strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..") )
{
listdir(p.c_str());
}
}//inner first if
else
std::cout << "ERROR in stat\n";
}//end while
closedir(dp);
}//first if
else
{
std::cout << "ERROR in opendir\n";
return 0;
}
return 1;
}//listdir()
最佳答案
你最大的问题似乎在这里:
sprintf(fpath,"%s\\%s",path,entry->d_name);
stat(fpath, &buf);
没有看到 fpath
的声明,很难确定,但你要么
在 sprintf
调用中溢出 fpath
,导致未定义的行为。 “系统卷信息”是一个很长的名字。你真的应该使用 snprintf
。
不检查 stat
调用的返回值。如果它返回 -1
,我不确定 buf
的内容是什么。
更重要的是,如果你可以使用 POSIX 的东西,函数 ftw
是标准的,应该提供您要在此处实现的大部分功能。
关于c++ - 如何在使用 dirent.h 阅读时跳过目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10392006/
我不明白为什么我的 C 编译器会抛出一个严重的错误。我根据 man page 初始化了所有东西,或者至少我认为我做了。 .然而我仍然被抛出一个严重的错误。它一直说 expected int (*)(c
我正在尝试编写一个程序来读取有关程序所在文件夹的信息。我已经仔细检查了其他在线资源,看起来我正在使用与其他正确解决方案相同的模式(见下文)。 #include #include #include
最终更新 - 答案位于已接受答案的评论中。 首先我意识到这个问题还有很多其他答案。我已经经历了其中的大部分,并且这段代码是经历了许多其他答案的组合。我想要做的就是获取目录中每个文件的完整路径。 #in
平台:Windows XP Service Pack 3 编译器:Code::Blocks 版本 12.11 我目前正在编写一个程序,该程序将使用 POSIX 目录函数递归删除给定目录。但我对 rea
你能告诉我,如何在下面的代码中只将文件名(没有目录)放入我的 vector 中吗?: int getDir (string dir, vector &files) { DIR *dp;
我已经编写了用于我在 Windows 上的应用程序的虚拟系统 (VFS)。现在,我将我的应用程序移至 iOS,但我遇到了 dirrent 问题。 Windows 端口添加了关于当前文件夹的信息,文件所
我正在 Ubunutu 16.04 linux 上开发一个 C++ 程序 它是从 shell 读取目录路径和组宽度。然后它应该在目录中导航并跟踪文件,如果它找到一个目录进入它并跟踪那些文件。并在末尾打
public:vector> cont; void memContent(string path, int f){ DIR *dir; struct dirent *ent; int
如果给定一个 std::string 形式的目录路径,我如何获得指向该目录的 dirent 结构? 我可以先使用 opendir() 获取所需目录的 DIR*。然后我可以在我的 DIR* 上使用 re
我尝试对文件夹中的文件进行计数,但 readdir 函数会跳过包含 unicode 字符的文件。我在 c 中使用 dirent。 int filecount(char* path) { int
我写了一个小函数,它接收一个文件夹的路径,并且应该返回其中的文件数。 出于某种原因,我的答案不正确:打印时我看到它打印了 . 和 ... 谁能告诉我为什么?减少 2 会解决问题吗? 代码如下:
我搜索了 dirent 以找到 C 例程 find / -iregex ".*/dirent.h$" 2>/dev/null 哪些返回相同的系列 /Library/Developer/CommandL
我有一个目录(我们称之为目录 A),其中包含约 15000 个目录(B1、B2、..、B15000),B 的每个目录都包含一个我想要的文件(我们称之为“原始”)读。所以我想阅读:A/B1/原始A/B2
我正在学习使用 dirent.h。虽然这个过程引人入胜且有趣,但我在使用 d_name 时遇到了问题。 我想使用 d_name 做两件事。 递归搜索子目录。对此,当我遇到DT_DIR类型的文件时,我会
有一些问题试图想出一个好方法来拥有一个只选择 wav 文件并专门返回这些文件的字符串的目录遍历器,例如只是wav文件。我可以停止 dirent.h 在子目录中查找,我很确定我只需要一个 if 语句,它
我正在构建一个需要 dirent.h 的 C++ 代码我发现这个文件已经被include了SYSROOT = $(NDK)/platforms/android-/arch-arm/ $SYSROOT/
我需要以单独的颜色打印 .c 文件、可执行文件和目录。请给我一些想法。提前致谢 int main(void) { DIR *d; int iNum = 0;
这个问题已经有答案了: Checking if a dir. entry returned by readdir is a directory, link or file. dent->d_type
我以前从未使用过dirent.h。我正在使用 istringstream 来读取文本文件(单数),但需要尝试修改程序以读取目录中的多个文本文件。这是我尝试实现 dirent 的地方,但它不起作用。 也
我正在尝试使用 dirent.h 中提供的功能递归打开文件 我的问题是:我无法跳过无法打开的目录。我希望它打开它可以打开的目录并跳过它不能打开的目录并移动到下一个目录而不是失败退出。我应该怎么做才能解
我是一名优秀的程序员,十分优秀!