S_ISDIR返回错误结果
来源:互联网 发布:php curl设置请求头 编辑:程序博客网 时间:2024/06/16 03:58
参考了2种遍历目录的写法
第一种:结果出错
int browse_dir(string dir)
{
DIR *pDir;
struct dirent *pEnt;
struct stat statbuf;
string sFileName;
pDir = opendir(dir.c_str());
if (pDir == NULL) {
if (errno == ENOTDIR) {
filenames.push_back(dir);
return 0;
}
perror(dir.c_str());
return -1;
}
while ((pEnt=readdir(pDir))!= NULL)
{
if ((pEnt->d_name[0]=='.')&&(pEnt->d_name[1]=='.' || pEnt->d_name[1]==0))
{
continue;
}
{
sFileName= dir + "/" + pEnt->d_name;
printf("directory->%s\n", sFileName.c_str());
// browse_dir(sFileName);
}
else
{
sFileName= dir +"/"+pEnt->d_name;
printf("file->%s\n",sFileName.c_str());
}
}
closedir(pDir);
return 0;
}
第2种写法:结果正确
void printdir(char *dir,int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp=opendir(dir))==NULL)
{
fprintf(stderr,"cannot open direntory:%s\n",dir);
return;
}
chdir(dir);
while((entry=readdir(dp))!=NULL)
{
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode))
{
if(strcmp(".",entry->d_name)==0||strcmp("..",entry->d_name)==0)
continue;
printf("%*s%s/\n",depth,"",entry->d_name);
printdir(entry->d_name,depth+4);
}
else printf("%*s%s\n",depth,"",entry->d_name);
}
chdir("..");
closedir(dp);
}
第一种:结果出错
int browse_dir(string dir)
{
DIR *pDir;
struct dirent *pEnt;
struct stat statbuf;
string sFileName;
pDir = opendir(dir.c_str());
if (pDir == NULL) {
if (errno == ENOTDIR) {
filenames.push_back(dir);
return 0;
}
perror(dir.c_str());
return -1;
}
while ((pEnt=readdir(pDir))!= NULL)
{
if ((pEnt->d_name[0]=='.')&&(pEnt->d_name[1]=='.' || pEnt->d_name[1]==0))
{
continue;
}
//lstat(pEnt->d_name,&statbuf);
//将sprintf(fullpath, "%s/%s", dir.c_str(), pEnt->d_name); //获取全局路径
lstat(fullpath,&statbuf);
{
sFileName= dir + "/" + pEnt->d_name;
printf("directory->%s\n", sFileName.c_str());
// browse_dir(sFileName);
}
else
{
sFileName= dir +"/"+pEnt->d_name;
printf("file->%s\n",sFileName.c_str());
}
}
closedir(pDir);
return 0;
}
第2种写法:结果正确
void printdir(char *dir,int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp=opendir(dir))==NULL)
{
fprintf(stderr,"cannot open direntory:%s\n",dir);
return;
}
chdir(dir);
while((entry=readdir(dp))!=NULL)
{
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode))
{
if(strcmp(".",entry->d_name)==0||strcmp("..",entry->d_name)==0)
continue;
printf("%*s%s/\n",depth,"",entry->d_name);
printdir(entry->d_name,depth+4);
}
else printf("%*s%s\n",depth,"",entry->d_name);
}
chdir("..");
closedir(dp);
}
0 0
- S_ISDIR返回错误结果
- 一些oj的返回结果:通过结果找错误,debug。
- S_ISDIR()函数
- S_ISDIR()函数
- 由S_ISDIR学到的
- S_ISDIR 常见的宏
- ibatis 返回结果集
- 调用通讯录返回结果
- Ibatis返回结果集
- 使用Callable返回结果
- 使用Callable返回结果
- informix返回结果集
- 浏览器返回结果解压
- 使用Callable返回结果
- sp_help返回结果描述
- sp_help返回结果描述
- startActivityForResult不返回结果
- 从Activity返回结果
- C++内存分配方式详解——堆、栈、自由存储区、全局/静态存储区和常量存储区
- ZigZag Conversion
- textarea中换行符存入数据库中为\r\n原因
- 关于音频处理
- 第三章 第二十一题
- S_ISDIR返回错误结果
- BT神器INI配置
- C++中的static关键字的总结
- iOS Crash文件的解析(二)如何使用symbolicatecrash工具
- 删除已排序数组中的重复元素
- CSUOJ 1601 War (离线并查集求连通块个数)
- 获取磁盘符信息
- 复杂的对话框
- 代理模式之Java动态代理