头文件
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
typedef enum fileType{
TYPE_FILE,
TYPE_FOLDER
}FileType;
typedef struct fileNode{
char fileName[1024];
struct fileNode * fileChild;
struct fileNode * nextSibling;
struct fileNode * parent;
}FileNode,*FileTree;
void PrintDir(char * dir,int depth);
主代码
#include "../Headers/FileTest.h"
int main(){
PrintDir("c:\\Test",0);
}
void PrintDir(char * dir,int depth){
DIR * dirPtr;
if ((dirPtr = opendir(dir)) == NULL){
sprintf(stderr,"无法打开目录:<%s>\n",dir);
}
struct dirent * dirContent;
while ((dirContent = readdir(dirPtr)) != NULL){
if (strcmp(dirContent->d_name,",") == 0 || strcmp(dirContent->d_name,"..") ==0){
continue;
}
char fullName[1024];
sprintf(fullName,"%s\\%s",dir,dirContent->d_name);
printf("当前文件名:%s\n",dirContent->d_name);
struct stat statInfo;
if ((stat(fullName,&statInfo)))
{
sprintf(stderr,"无法获取文件<%s>的详细信息!\n",fullName);
}
if ((statInfo.st_mode & S_IFMT) == S_IFDIR){
printf("\t是目录!\n");
PrintDir(fullName,++depth);
}else if ((statInfo.st_mode & S_IFMT) == S_IFREG){
printf("\t是一般文件,所占空间:%ld\n",statInfo.st_size);
}
}
}