#include <stdio.h>
#include <sys/dir.h>
#include <string>
#include <sys/stat.h>
//判断是否为文件夹
bool isDir(const char* path);
//遍历文件夹的驱动函数
void findFiles(const char *path);
//遍历文件夹de递归函数
void findFiles(const char *path, int recursive);
#define MAX_LEN 1024 * 512
int main(int argc, const char * argv[])
{
findFiles("未命名文件夹");
return 0;
}
//判断是否为目录
bool isDir(const char* path)
{
struct stat st;
lstat(path, &st);
return 0 != S_ISDIR(st.st_mode);
}
//遍历文件夹的驱动函数
void findFiles(const char *path)
{
unsigned long len;
char temp[MAX_LEN];
//去掉末尾的'/'
len = strlen(path);
strcpy(temp, path);
if(temp[len - 1] == '/') temp[len -1] = '\0';
if(isDir(temp))
{
//处理目录
int recursive = 1;
findFiles(temp, recursive);
}
else //输出文件
{
printf("====&
linux下 c语言递归遍历文件夹下所有文件和子文件夹(附上替换文本文件内容的方法)
最新推荐文章于 2021-05-23 23:04:09 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)