linux下 c语言递归遍历文件夹下所有文件和子文件夹(附上替换文本文件内容的方法)

该博客提供了一个C语言程序,用于在Linux环境下递归遍历文件夹及其子文件夹,并展示了一个替换文本文件中特定字符串的实例。程序通过`ifstream`和`ofstream`读写文件,使用`find`函数查找并替换目标字符串。
摘要由CSDN通过智能技术生成

#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("====&
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值