经典错误,请高人帮忙分析-linux下统计当前目录下的子目录数(当前目录)与其各子目录下的文件数?
有目录结构:
./a/aa/aaa/1.txt
./a/aa/aaa/2.txt
./a/aa/aaa/3.txt
./a/aa/bbb/1.txt
./a/aa/bbb/2.txt
./a/aa/bbb/3.txt
./a/aa/ccc/1.txt
./a/aa/ccc/2.txt
./a/aa/ccc/3.txt
我用的实现while循环下加嵌套函数。
按上面目录结构输出正确结果应该为:文件数9个 目录数6
可我的程序结果为:文件数3个 目录数4个(bbb ccc2个目录及其下面的6个文件未统计)
完整代码如下:
#include <iostream>
using namespace std;
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
int Fn=0;
int Dn=0;
DIR* p_Dir = NULL;
int count(char *);
int main(int argc,char* argv[]){
if(argc!=2){
cout <<"Usage:<filename> DirName"<<endl;
return -1;
}
p_Dir = opendir(argv[1]);
if(NULL==p_Dir){
cout <<"Can't find the Dir:"<<argv[1]<<endl;
return -2;
}
else{
closedir(p_Dir);
}
p_Dir=NULL;
count(argv[1]);//函授调用
cout <<"The director include: "<<Fn<<" files and "<<Dn<<" directors."<<endl;
}
//函数定义
int count(char* s_dir){
struct stat buf;
struct dirent* p_Dirent = NULL;
if(stat(s_dir,&buf)<0){
cout << "stat error\n";
return -2;
}
if(S_ISDIR(buf.st_mode)){
if(NULL==(p_Dir = opendir(s_dir))){
cout << "Open "<<s_dir<<" director failure."<<endl;
return -1;
}
while(p_Dirent = readdir(p_Dir)){
if('.'==(p_Dirent->d_name[0])) continue;
char str[256];
memset(str,0,256);
strcpy(str,s_dir);
strcat(str,"/");
strcat(str,p_Dirent->d_name);
cout <<"found dir: " <<str<<endl;
count(str); //嵌套调用
}
Dn++;
return 0;
}
else{
Fn++;
return 0;
}
}
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/dir.h>
int isdir(const char* path)
{
struct stat st;
lstat(path, &st);
return S_ISDIR(st.st_mode);
}
int cdf(const char* path, int* dc, int* fc)
{
DIR* dir;
struct dirent* drt;
char filename[256];
if((dir = opendir(path)) == NULL)
return -1;
*dc = *dc + 1;
while((drt = readdir(dir)) != NULL)
{
if(strcmp(drt->d_name, ".") == 0
[其他解释]
这里有个用迭代删除文件夹的例子。可以参考下:
删除文件夹的办法
[其他解释]
获取当前目录的子目录数目不需要嵌套的,还是说你的需求描述的不够清楚。
[其他解释]
你这个为什么没有处理 ..来,只掠过了当前目录,但是没有略过上层目录,目测是个死循环,并且,你只统计目录,其他的I_IS...来,像是普通文件,CHR,SOCK,PIPE,FIFO,DEV都木有啊有木有,所以应该写的全一点
[其他解释]
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/dir.h>
int isdir(const char* path)
{
struct stat st;
lstat(path, &st);
return S_ISDIR(st.st_mode);
}
int cdf(const char* path, int* dc, int* fc)
{
DIR* dir;
struct dirent* drt;
char filename[256];
if((dir = opendir(path)) == NULL)
return -1;
*dc = *dc + 1;
while((drt = readdir(dir)) != NULL)
{
if(strcmp(drt->d_name, ".") == 0
[其他解释]
strcmp(drt->d_name, "..") == 0)
continue;
sprintf(filename, "%s/%s", path, drt->d_name);
printf("%s\n", filename);
if(isdir(filename)){
if(cdf(filename, dc, fc) != 0)
return -1;
}
else
*fc = *fc + 1;
}
closedir(dir);
return 0;
}
//检查输入的pathname的末尾是否有‘/’,如果有去掉.但如果输入了2个'/'在路径末尾就没办法了,我这里懒一下没用字符查找函数
int fslash(char* pathname){
int len = strlen(pathname);
if(len>0){
if(pathname[len-1]=='/'){
pathname[len-1]='\0';
return 0;
}
}else{
printf("pathname is to less");
return -1;
}
}
int main(int argc, char* argv[])
{
int dc = 0, fc = 0;
if(argc != 2)
{
printf("Usage: cdf path\n");
return -1;
}
fslash(argv[1]);
if(cdf(argv[1], &dc, &fc) != 0)
return -1;
printf("dc=%d, fc=%d\n", dc, fc);
return 0;
}