下载 FreeRTOS
 

出色的 RTOS & 嵌入式软件

最新资讯
简化任何设备的身份验证云连接。
利用 CoAP 设计节能型云连接 IoT 解决方案。
11.0.0 版 FreeRTOS 内核简介:
FreeRTOS 路线图和代码贡献流程。
使用 FreeRTOS 实现 OPC-UA over TSN。

ff_findfirst()

[FreeRTOS-Plus-FAT 标准 API 引用]

ff_stdio.h
int ff_findfirst( const char *pcDirectory, ff_finddata_t *pxFindData );
		

在嵌入式 FAT 文件系统的目录中查找第一个文件。

ff_findfirst() 与 ff_findnext() 一起使用 扫描目录,以查找目录包含的所有文件。

由于其大小相对而言较大,建议动态分配 FF_FindData_t 结构体, 而不是声明为堆栈 变量。 该结构体在使用前,也必须 清零。 请参阅以下示例。

FF_FindData_t 包含的字段如下表所示:

字段
说明
pcFileName 文件的名称
ulFileSize 文件长度(以字节为单位)
ucAttributes 文件属性,是按位或以下位 定义的:
  • FF_FAT_ATTR_READONLY
  • FF_FAT_ATTR_HIDDEN
  • FF_FAT_ATTR_SYSTEM
  • FF_FAT_ATTR_DIR (directory)

参数:

pcDirectory   指向以 null 结尾的标准 C 字符串的指针,该字符串包含 要在其中找到第一个文件的 目录的名称。 尚不支持文件通配符, 因此字符串应仅包含目录名称。 例如, 要使用当前工作目录,请使用 空字符串 ( "" ),请勿使用 ("*.*")。

pxFindData   指向一个结构体的指针, 该结构体用于存储扫描目录所需的信息, 并传递目录中所包含文件的详细信息。
返回:

如果找到文件或目录,则返回 0。 如果发生错误 则返回非零值。

用法示例:


void DIRCommand( const char *pcDirectoryToScan )
{
FF_FindData_t *pxFindStruct;
const char *pcAttrib;
*pcWritableFile = "writable file",
*pcReadOnlyFile = "read only file",
*pcDirectory = "directory";

/* FF_FindData_t can be large, so it is best to allocate the structure
dynamically, rather than declare it as a stack variable. */

pxFindStruct = ( FF_FindData_t * ) pvPortMalloc( sizeof( FF_FindData_t ) );

/* FF_FindData_t must be cleared to 0. */
memset( pxFindStruct, 0x00, sizeof( FF_FindData_t ) );

/* The first parameter to ff_findfist() is the directory being searched. Do
not add wildcards to the end of the directory name. */

if( ff_findfirst( pcDirectoryToScan, pxFindStruct ) == 0 )
{
do
{
/* Point pcAttrib to a string that describes the file. */
if( ( pxFindStruct->ucAttributes & FF_FAT_ATTR_DIR ) != 0 )
{
pcAttrib = pcDirectory;
}
else if( pxFindStruct->ucAttributes & FF_FAT_ATTR_READONLY )
{
pcAttrib = pcReadOnlyFile;
}
else
{
pcAttrib = pcWritableFile;
}

/* Print the files name, size, and attribute string. */
FreeRTOS_printf( ( "%s [%s] [size=%d]", pxFindStruct->pcFileName,
pcAttrib,
pxFindStruct->ulFileSize ) );

} while( ff_findnext( pxFindStruct ) == 0 );
}

/* Free the allocated FF_FindData_t structure. */
vPortFree( pxFindStruct );
}

Example use of the ff_findfirst() API function create a directory listing


Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.