下载 FreeRTOS
 

出色的 RTOS & 嵌入式软件

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

ff_fgets()

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

ff_stdio.h
char *ff_fgets( char *pcBuffer, size_t xCount, FF_FILE *pxStream );
		

从文件中读取字符串的步骤为:将字节从 pxStream 读入 pcBuffer,直至读取了 (xCount - 1) 个字节, 或直至换行符 ('n') 已读入 pcBuffer。

回车字符 ('r') 不以任何特殊方式处理,并且 会复制到 pcBuffer 中。

复制到 pcBuffer 中的字符串在 ff_fgets() 返回之前为 NULL。

参数:

pcBuffer   指向缓冲区的指针,从文件读取的字符 将被放置在该缓冲区中。 缓冲区必须至少 足够容纳 xCount 字节。

xCount   将从文件读取字节,直至接收到 换行符,或已读取 (xCount - 1) 个字节。

pxStream   指向数据待读取的文件的指针。 该指针与调用 ff_fopen() 返回的指针相同,最初用于打开文件。

返回:

如果成功,返回指向 pcBuffer 的指针。 如果有读取错误 则返回 NULL,并且任务的 errno 被设置为指示原因。 任务可以使用 stdioGET_ERRNO() API 函数获取其 errno 值。

用法示例:


static void prvTest_ff_fgets_ff_printf( const char *pcMountPath )
{
FF_FILE *pxFile;
int iString;
const int iMaxStrings = 1000;
char pcReadString[ 20 ], pcExpectedString[ 20 ], *pcReturned;
const char *pcMaximumStringLength = "Test string 999n";

/* Open a file for reading and writing. */
pxFile = ff_fopen( "/nand/myfile.txt", "w+" );

/* Use ff_fprintf() to write some strings to the file. The strings are
generated as "Test string nnnn", where nnn is the loop counter. */

for( iString = 0; iString < iMaxStrings; iString++ )
{
/* Call ff_fprintf() to write the formatted string to the file. Note
the n character on the end of the string. */

ff_fprintf( pxFile, "Test string %dn", iString );
}

/* Move back to the start of the file. */
ff_rewind( pxFile );

/* This time use the ff_fgets() string to read back each string at a time,
then compare it against the expected string. The strings were written with
a newline character at their end, so ff_fgets() will read up to and
including the newline. */

for( iString = 0; iString < iMaxStrings; iString++ )
{
/* Read back the next string. */
pcReturned = ff_fgets( pcReadString, sizeof( pcReadString ), pxFile );

if( pcReturned != pcReadString )
{
/* Error! */
}
else
{
/* Generate the string that is expected to have been read back. */
sprintf( pcExpectedString, "Test string %dn", iString );

/* Compare the string that was expected to be returned against the
string that was returned. */

if( strcmp( pcExpectedString, pcReadString ) == 0 )
{
/* The strings matched, as expected. */
}
else
{
/* Error - the strings didn't match. */
}
}
}

ff_fclose( pxFile );
}

Example use of the ff_fgets() API function
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.