C语言 遍历一个文件夹的所有文件以及函数strstri不区分大小写查找字符串

2022-05-20 16:31:48 浏览数 (1)

一、遍历一个文件夹的所有文件代码

代码语言:javascript复制
int  listallfilename(const char* path)
{
    DIR    *dir;
    struct    dirent    *ptr;
    dir = opendir(path);
    while((ptr = readdir(dir)) != NULL)
    {
        printf("path=%s,d_name: %sn",path,ptr->d_name);
    }
    closedir(dir);
    return 0;
}

二、函数strstri不区分大小写查找字符串c代码

代码语言:javascript复制
char* strstri(char * inBuffer, char * inSearchStr)
{
    char*  currBuffPointer = inBuffer;
 
    while (*currBuffPointer != 0x00)
    {
        char* compareOne = currBuffPointer;
        char* compareTwo = inSearchStr;
        //统一转换为小写字符
        while (tolower(*compareOne) == tolower(*compareTwo))
        {
            compareOne  ;
            compareTwo  ;
            if (*compareTwo == 0x00)
            {
                return (char*) currBuffPointer;
            }
 
        }
        currBuffPointer  ; 
    }
    return NULL;
}

三、实际应用测试程序

代码语言:javascript复制
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
 
struct rk3288_display_devices
{
    bool hdmi;
    bool lvds;
    bool mipi;
    bool edp;
};
 
struct rk3288_display_devices m_display_devices={0,0,0,0};
 
char* strstri( char*  inBuffer,  const char* inSearchStr)
{
    const char*  currBuffPointer = inBuffer;
 
    while (*currBuffPointer != 0x00)
    {
        const char* compareOne = currBuffPointer;
        const char* compareTwo = inSearchStr;
        while (tolower(*compareOne) == tolower(*compareTwo))
        {
            compareOne  ;
            compareTwo  ;
            if (*compareTwo == 0x00)
            {
                return (char*) currBuffPointer;
            }
 
        }
        currBuffPointer  ; 
    }
    return NULL;
}
 
 
int  check_rk3288_drm_display_devices(const char* path)
{
    DIR    *dir;
    struct    dirent    *ptr;
    dir = opendir(path);
    while((ptr = readdir(dir)) != NULL)
    {
        printf("path=%s,d_name: %sn",path,ptr->d_name);
        if(strstri(ptr->d_name,"hdmi"))
        {
            m_display_devices.hdmi=true;
            printf("hdmi rn");
        }else if(strstri(ptr->d_name,"mipi"))
        {
            m_display_devices.mipi=true;
            printf("mipi rn");
        }else if(strstri(ptr->d_name,"lvds"))
        {
            m_display_devices.lvds=true;
            printf("lvds rn");
        }
        else if(strstri(ptr->d_name,"edp"))
        {
            m_display_devices.edp=true;
            printf("edp rn");
        }    
    }
    closedir(dir);
    return 0;
}
int main(){
    check_rk3288_drm_display_devices("/sys/class/drm/");
    return 0;
 
}

四、android机子运行结果

xxx:/ # test_c

path=/sys/class/drm/,d_name: .

path=/sys/class/drm/,d_name: ..

path=/sys/class/drm/,d_name: card0-HDMI-A-1

hdmi

path=/sys/class/drm/,d_name: card0

path=/sys/class/drm/,d_name: renderD128

path=/sys/class/drm/,d_name: card0-LVDS-1

lvds

path=/sys/class/drm/,d_name: controlD64

path=/sys/class/drm/,d_name: version

xxx:/ # ls -a /sys/class/drm/

. .. card0 card0-HDMI-A-1 card0-LVDS-1 controlD64 renderD128 version

xxx:/ #

0 人点赞