findfirst,strncasecmp函数

本文详细介绍了findfirst函数的用法及其在Windows系统中的应用实例,同时对比了Linux环境下类似功能的实现方式。此外,还深入解析了strncasecmp函数的功能与用法,包括其语法、返回值及实际应用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

findfirst函数的用法
函数名称:     findfirst
函数原型:     int findfirst(char *fname,struct ffblk *ptr,int attrib)
函数功能:     寻找与fname相匹配的第一个文件名称
函数返回:
参数说明:     ptr-保存查找到的文件信息
所属文件:    <dir.h >

#include   <stdio.h >
#include   <dir.h >
int main()
{
struct ffblk ffblk;
int done;
printf("Directory listing of *.*");
done=findfirst("*.*",&ffblk,0);
while (!done)
    {
       printf("%s", ffblk.ff_name);
       done=findnext(&ffblk);
}
return 0;
}

int findfirst(char *pathname,struct ffblk *ffblk,int attrib)查找指定的文件,成功
    返回0
    pathname为指定的目录名和文件名,如"C://WPS//TXT"
    ffblk为指定的保存文件信息的一个结构,定义如下:
┏━━━━━━━━━━━━━━━━━━┓
┃struct ffblk                      ┃
┃{                                   ┃
┃ char ff_reserved[21]; /*DOS保留字*/┃
┃ char ff_attrib;    /*文件属性*/ ┃
┃ int   ff_ftime;        /*文件时间*/ ┃
┃ int   ff_fdate;        /*文件日期*/ ┃
┃ long ff_fsize;        /*文件长度*/ ┃
┃ char ff_name[13];     /*文件名*/ ┃
┃}                                   ┃
┗━━━━━━━━━━━━━━━━━━┛
    attrib为文件属性,由以下字符代表
┏━━━━━━━━━┳━━━━━━━━┓
┃FA_RDONLY 只读文件┃FA_LABEL   卷标号┃
┃FA_HIDDEN 隐藏文件┃FA_DIREC   目录   ┃
┃FA_SYSTEM 系统文件┃FA_ARCH 档案   ┃
┗━━━━━━━━━┻━━━━━━━━┛
例:
struct ffblk ff;
findfirst("*.wps",&ff,FA_RDONLY);
 
这只限于windows ,而在linux下使用打开文件夹 要用opendir ,头文件是dirent.h

 

 

 

Definition and Usage
定义和用法

The strncasecmp() function compares two strings.
strncasecmp()函数的作用是:比较字符串的前n个字符(大小写不敏感)。

This function returns:
这个函数将返回下列值:

  • 0 - if the two strings are equal
    0 – 如果字符串相等
  • <0 - if string1 is less than string2
    <0 – 如果string1小于string2
  • >0 - if string1 is greater than string2
    >0 – 如果string1大于string2

Syntax
语法

strncasecmp(string1,string2,length)

Parameter参数Description描述
string1Required. Specifies the first string to compare
必要参数。指定参与比较的第一个字符串对象
string2Required. Specifies the second string to compare
必要参数。指定参与比较的第二个字符串对象
lengthRequired. Specify the number of characters from each string to be used in the comparison
必要参数。指定每个字符串中参数比较的字符数量

Tips and Notes
注意点

Note: The strncasecmp() is binary safe and case-insensitive.
注意:strncasecmp()函数是二进制精确的,并且它不区分字母大小写。


Example
案例

<?php
echo strncasecmp("Hello world!","hello earth!",6);
?>

The output of the code above will be:
上述代码将输出下面的结果:

0
// 字符串比较函数(用于排序) #define PATH_MAX 4096 // 比较函数用于排序 static int compare_strings(const void *a, const void *b) { return strcasecmp(*(const char **)a, *(const char **)b); } // 补全状态结构 typedef struct { char **matches; // 匹配路径数组 int match_count; // 匹配数量 int current_index; // 当前索引 char *base_text; // 初始化时的文本 } CompletionState; static CompletionState comp_state = {0}; // 重置状态 void reset_completion_state() { printf("Resetting completion state\n"); if (comp_state.matches) { for (int i = 0; i < comp_state.match_count; i++) { if (comp_state.matches[i]) { XFREE(MTYPE_TMP, comp_state.matches[i]); } } XFREE(MTYPE_TMP, comp_state.matches); comp_state.matches = NULL; } if (comp_state.base_text) { XFREE(MTYPE_TMP, comp_state.base_text); comp_state.base_text = NULL; } comp_state.match_count = 0; comp_state.current_index = 0; } // 文件补全函数 char *filename_completion_function(const char *text, int state) { printf("\nfilename_completion_function: state=%d text=%s\n", state, text); // 状态0:初始化或文本变化时重新初始化 if (state == 0 || (comp_state.base_text && strcmp(text, comp_state.base_text) != 0)) { printf("Initializing new completion for: %s\n", text); reset_completion_state(); // 保存当前文本作为基准 comp_state.base_text = XSTRDUP(MTYPE_TMP, text); printf("Set base_text: %s\n", comp_state.base_text); // 解析基础路径和前缀 const char *last_slash = strrchr(text, '/'); char base_path[PATH_MAX] = {0}; const char *prefix = text; if (last_slash) { // 提取目录部分(包括结尾斜杠) int base_len = last_slash - text + 1; if (base_len >= PATH_MAX) base_len = PATH_MAX - 1; strncpy(base_path, text, base_len); base_path[base_len] = '\0'; prefix = last_slash + 1; printf("Parsed: base_path='%s', prefix='%s'\n", base_path, prefix); } else { // 没有斜杠时,base_path 应为空字符串 base_path[0] = '\0'; prefix = text; printf("No slash: base_path='', prefix='%s'\n", prefix); } // 确定要打开的目录路径 const char *dir_to_open = (base_path[0] == '\0') ? "." : base_path; printf("Opening directory: %s\n", dir_to_open); // 打开目录 DIR *dir = opendir(dir_to_open); if (!dir) { printf("Cannot open directory: %s (error: %s)\n", dir_to_open, strerror(errno)); return NULL; } // 临时存储匹配项 char *temp_matches[256] = {0}; int count = 0; size_t prefix_len = strlen(prefix); struct dirent *entry; printf("Scanning directory entries...\n"); while ((entry = readdir(dir)) != NULL && count < 255) { // 跳过特殊目录 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } // 前缀匹配检查 if (prefix_len > 0 && strncasecmp(entry->d_name, prefix, prefix_len) != 0) { continue; } // 判断是否为目录 int is_dir = (entry->d_type == DT_DIR); // 构建完整路径 int path_len = strlen(base_path) + strlen(entry->d_name) + (is_dir ? 1 : 0) + 1; char *path = XMALLOC(MTYPE_TMP, path_len); // 直接拼接路径 if (base_path[0] != '\0') { snprintf(path, path_len, "%s%s%s", base_path, entry->d_name, is_dir ? "/" : ""); } else { snprintf(path, path_len, "%s%s", entry->d_name, is_dir ? "/" : ""); } printf("Found match: %s\n", path); temp_matches[count++] = path; } closedir(dir); // 没有匹配项 if (count == 0) { printf("No matches found\n"); return NULL; } // 排序匹配项 qsort(temp_matches, count, sizeof(char *), compare_strings); // 复制到状态 comp_state.matches = XCALLOC(MTYPE_TMP, (count + 1) * sizeof(char *)); for (int i = 0; i < count; i++) { comp_state.matches[i] = temp_matches[i]; } comp_state.match_count = count; comp_state.current_index = 0; printf("Initialized with %d matches\n", count); } // 返回匹配项或NULL if (comp_state.current_index < comp_state.match_count) { char *match = comp_state.matches[comp_state.current_index]; char *result = XSTRDUP(MTYPE_TMP, match); printf("Returning match[%d/%d]: %s\n", comp_state.current_index, comp_state.match_count, result); comp_state.current_index++; return result; } // 重置索引以便循环 printf("Resetting index for next cycle\n"); comp_state.current_index = 0; // 返回NULL表示结束 return NULL; } <dahua>dir e cmlsh_completion: enter----start = 4, end = 5 filename_completion_function: state=0 text=e Initializing new completion for: e Resetting completion state Set base_text: e No slash: base_path='', prefix='e' Opening directory: . Scanning directory entries... Found match: etc/ Initialized with 1 matches Returning match[0/1]: etc/ filename_completion_function: state=1 text=e Resetting index for next cycle filename_completion_matches: Found 1 matches for 'e': [0] etc/ tc/ cmlsh_completion: enter----start = 4, end = 8 filename_completion_function: state=0 text=etc/ Initializing new completion for: etc/ Resetting completion state Set base_text: etc/ Parsed: base_path='etc/', prefix='' Opening directory: etc/ Scanning directory entries... Found match: etc/ssh/ Found match: etc/nos.conf Found match: etc/CML_DB.db Initialized with 3 matches Returning match[0/3]: etc/CML_DB.db filename_completion_function: state=1 text=etc/ Returning match[1/3]: etc/nos.conf filename_completion_function: state=2 text=etc/ Returning match[2/3]: etc/ssh/ filename_completion_function: state=3 text=etc/ Resetting index for next cycle filename_completion_matches: Found 3 matches for 'etc/': [0] etc/CML_DB.db [1] etc/nos.conf [2] etc/ssh/ CML_DB.db cmlsh_completion: enter----start = 4, end = 17 filename_completion_function: state=0 text=etc/CML_DB.db Initializing new completion for: etc/CML_DB.db Resetting completion state Set base_text: etc/CML_DB.db Parsed: base_path='etc/', prefix='CML_DB.db' Opening directory: etc/ Scanning directory entries... Found match: etc/CML_DB.db Initialized with 1 matches Returning match[0/1]: etc/CML_DB.db filename_completion_func Username:
07-31
all: make install -C src make -C test clean: make clean -C src make clean -C test -rm posix -rf menuconfig: make -C test menuconfig 这是我的顶层makefile,以下是我test的makefile,我是需要在顶层目录输入make menuconfig可以启动test里面的make menuconfig,在并且在配置过后,下次使用make不打开menuconfig,如果没有配置文件的话,才需要打开配置界面去配置。。现在是无法正确生成..h文件,需要怎么修改呢? # 从当前用户主目录查找目录的函数 # 参数1: 要查找的目录名 define find_in_home $(shell \ found_dir=$$(find "$$HOME" -type d -name "$(1)" -print -quit 2>/dev/null); \ if [ -n "$$found_dir" ]; then \ echo "$$found_dir"; \ else \ echo "DIRECTORY_NOT_FOUND"; \ fi \ ) endef # 使用示例(查找用户主目录下的 Documents 目录) TARGET_DIR := lite-user-runtime-env ENV_DIR := $(call find_in_home,$(TARGET_DIR)) ifeq ($(ENV_DIR),DIRECTORY_NOT_FOUND) $(error Directory "$(TARGET_DIR)" not found!!) else $(info ENV_DIR = $(ENV_DIR)) endif # Kconfig 配置系统相关 KCONFIG_CONFIG ?= $(CURDIR)/.config KCONFIG_AUTOHEADER ?= $(CURDIR)/autoconf.h KCONFIG_AUTOCONF ?= $(CURDIR)/autoconf.h # 检查 Kconfig 工具是否存在 ifeq ($(shell command -v kconfig-mconf 2>/dev/null),) $(error "kconfig-frontends not found, please install it first (sudo apt install kconfig-frontends)") endif CROSS = $(ENV_DIR)/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi- CREAT_TOOL = $(ENV_DIR)/bin/create-sp-img CC = gcc INC = -I . -I ./include -I ../posix/include INC += -I $(ENV_DIR)/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/include #cflag = -g ${cflags} -Wall -fPIC -fno-asynchronous-unwind-tables -fno-builtin-function -mcpu=cortex-m3 -mthumb -nostdinc #inc += -I $(SPBASEDIR)/vsoc/$(os_type)/include/ -I . #inc += -I $(SPBASEDIR)/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/include # CFLAG = -g -Wall -fPIC -fno-asynchronous-unwind-tables -mcpu=cortex-a5 -mtune=generic-armv7-a -mthumb -mfloat-abi=soft -Os # CFLAG = -g -Os -Wall -fPIC -fno-asynchronous-unwind-tables -mcpu=cortex-a5 -mtune=generic-armv7-a -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard #-ffreestanding 独立环境 CFLAG += -include $(KCONFIG_AUTOHEADER) CFLAG += -Os -fno-asynchronous-unwind-tables -mcpu=cortex-m3 -mthumb -nostdinc -nostdlib -ffreestanding -ffunction-sections -fdata-sections CFLAG += -fPIE -Wall -fno-builtin-function -Werror=implicit -flto #lflag = -g -pie -T n58.ld.lds -Os --no-warn-rwx-segments --start-group $(ENV_DIR)/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/thumb/v7-m/nofp/libgcc.a ../posix/lib/libposix.a --end-group #lflag = ../src/raw.o ../src/dns_server.o ../src/exit.o ../src/memset.o ../src/network.o ../src/strncasecmp.o ../src/timerfd.o ../src/vfs_device.o ../src/vfs.o lflag += -g -pie -T n58.ld.lds -Os -Wl,--no-warn-rwx-segments -flto -mcpu=cortex-m3 -mthumb -nostartfiles -nostdlib lflag += -Wl,--start-group,../posix/lib/libposix.a,$(ENV_DIR)/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/thumb/v7-m/nofp/libgcc.a,--end-group lflag += -Wl,-Bsymbolic,-gc-sections LD = gcc objs = main.o websocket_client.o websocket_client_posix.o uart_audio.o crc32_new.o hdlc_byte.o var_buffer_fifo.o tiny_json.o gpio.o led.o pwrkey.o adc.o rtc.o target = test image = $(target).spimg # Kconfig 配置目标:打开图形化配置界面 menuconfig: Kconfig kconfig-mconf $< --output $(KCONFIG_CONFIG) # 生成配置头文件(从 .config 生成 config.h) $(KCONFIG_AUTOHEADER): $(KCONFIG_CONFIG) @if [ ! -f "$(KCONFIG_CONFIG)" ]; then \ $(MAKE) menuconfig; \ fi kconfig-conf --silentoldconfig $(CURDIR)/Kconfig $(KCONFIG_CONFIG) @echo "Generated $(KCONFIG_AUTOHEADER)" .c.o: $(CROSS)$(CC) -c $< $(INC) $(CFLAG) all:$(KCONFIG_AUTOHEADER) $(target) $(image) $(target):$(objs) $(CROSS)$(LD) -o $@ $^ $(lflag) $(image):$(target) $(CREAT_TOOL) $(target) host armm3 > /dev/null clean: -rm -fv $(target) -rm -fv *.o -rm -fv *.spimg
最新发布
08-08
# 从当前用户主目录查找目录的函数 # 参数1: 要查找的目录名 define find_in_home $(shell \ found_dir=$$(find "$$HOME" -type d -name "$(1)" -print -quit 2>/dev/null); \ if [ -n "$$found_dir" ]; then \ echo "$$found_dir"; \ else \ echo "DIRECTORY_NOT_FOUND"; \ fi \ ) endef # 使用示例(查找用户主目录下的 Documents 目录) TARGET_DIR := lite-user-runtime-env ENV_DIR := $(call find_in_home,$(TARGET_DIR)) ifeq ($(ENV_DIR),DIRECTORY_NOT_FOUND) $(error Directory "$(TARGET_DIR)" not found!!) else $(info ENV_DIR = $(ENV_DIR)) endif # Kconfig 配置系统相关 KCONFIG_CONFIG ?= $(CURDIR)/.config KCONFIG_AUTOHEADER ?= $(CURDIR)/autoconf.h KCONFIG_AUTOCONF ?= $(CURDIR)/autoconf.h # 检查 Kconfig 工具是否存在 ifeq ($(shell command -v kconfig-mconf 2>/dev/null),) $(error "kconfig-frontends not found, please install it first (sudo apt install kconfig-frontends)") endif CROSS = $(ENV_DIR)/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi- CREAT_TOOL = $(ENV_DIR)/bin/create-sp-img CC = gcc INC = -I . -I ./include -I ../posix/include INC += -I $(ENV_DIR)/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/include #cflag = -g ${cflags} -Wall -fPIC -fno-asynchronous-unwind-tables -fno-builtin-function -mcpu=cortex-m3 -mthumb -nostdinc #inc += -I $(SPBASEDIR)/vsoc/$(os_type)/include/ -I . #inc += -I $(SPBASEDIR)/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/include # CFLAG = -g -Wall -fPIC -fno-asynchronous-unwind-tables -mcpu=cortex-a5 -mtune=generic-armv7-a -mthumb -mfloat-abi=soft -Os # CFLAG = -g -Os -Wall -fPIC -fno-asynchronous-unwind-tables -mcpu=cortex-a5 -mtune=generic-armv7-a -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard #-ffreestanding 独立环境 CFLAG += -include $(KCONFIG_AUTOHEADER) CFLAG += -Os -fno-asynchronous-unwind-tables -mcpu=cortex-m3 -mthumb -nostdinc -nostdlib -ffreestanding -ffunction-sections -fdata-sections CFLAG += -fPIE -Wall -fno-builtin-function -Werror=implicit -flto #lflag = -g -pie -T n58.ld.lds -Os --no-warn-rwx-segments --start-group $(ENV_DIR)/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/thumb/v7-m/nofp/libgcc.a ../posix/lib/libposix.a --end-group #lflag = ../src/raw.o ../src/dns_server.o ../src/exit.o ../src/memset.o ../src/network.o ../src/strncasecmp.o ../src/timerfd.o ../src/vfs_device.o ../src/vfs.o lflag += -g -pie -T n58.ld.lds -Os -Wl,--no-warn-rwx-segments -flto -mcpu=cortex-m3 -mthumb -nostartfiles -nostdlib lflag += -Wl,--start-group,../posix/lib/libposix.a,$(ENV_DIR)/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/thumb/v7-m/nofp/libgcc.a,--end-group lflag += -Wl,-Bsymbolic,-gc-sections LD = gcc objs = main.o websocket_client.o websocket_client_posix.o uart_audio.o crc32_new.o hdlc_byte.o var_buffer_fifo.o tiny_json.o gpio.o led.o pwrkey.o adc.o rtc.o target = test image = $(target).spimg # Kconfig 配置目标:打开图形化配置界面 menuconfig: Kconfig kconfig-mconf $< --output $(KCONFIG_CONFIG) # 生成配置头文件(从 .config 生成 config.h) $(KCONFIG_AUTOHEADER): $(KCONFIG_CONFIG) @if [ ! -f "$(KCONFIG_CONFIG)" ]; then \ $(MAKE) menuconfig; \ fi kconfig-conf --silentoldconfig $(CURDIR)/Kconfig $(KCONFIG_CONFIG) @echo "Generated $(KCONFIG_AUTOHEADER)" .c.o: $(CROSS)$(CC) -c $< $(INC) $(CFLAG) all:$(KCONFIG_AUTOHEADER) $(target) $(image) $(target):$(objs) $(CROSS)$(LD) -o $@ $^ $(lflag) $(image):$(target) $(CREAT_TOOL) $(target) host armm3 > /dev/null clean: -rm -fv $(target) -rm -fv *.o -rm -fv *.spimg 以上是我test的makefile,我顶层的makefile是all: make install -C src make -C test clean: make clean -C src make clean -C test -rm posix -rf menuconfig: make -C test menuconfig 我现在需要,没有.h文件时,打开menuconfig进行配置,有时则不打开menuconfig,延用之前的配置,但是现在不能生成.h文件,但是能生成.config文件,怎么办
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值