FATFS 初学之 f_gets/ f_putc/ f_puts/ f_printf

本文介绍了在嵌入式系统中使用FATFS库进行文件操作的四个基本函数:f_gets用于从文件读取字符串,f_putc用于写入单个字符,f_puts用于写入字符串,f_printf则用于格式化输出字符串。这些函数在不同的模式下有不同的行为,如' '的处理和' '的去除。
摘要由CSDN通过智能技术生成

详见:嵌入式大讲堂

 

f_gets:

 1 /*-----------------------------------------------------------------------*/
 2 /* Get a string from the file                                            */
 3 /*-----------------------------------------------------------------------*/
 4 TCHAR* f_gets (
 5     TCHAR* buff,    /* Pointer to the string buffer to read */
 6     int len,        /* Size of string buffer (characters) */
 7     FIL* fil        /* Pointer to the file object */
 8 )
 9 {
10     int n = 0;
11     TCHAR c, *p = buff;
12     BYTE s[2];
13     UINT rc;
14 
15 
16     while (n < len - 1) {            /* Read bytes until buffer gets filled */
17         f_read(fil, s, 1, &rc);
18         if (rc != 1) break;            /* Break on EOF or error */
19         c = s[0];
20 #if _LFN_UNICODE                    /* Read a character in UTF-8 encoding */
21         if (c >= 0x80) {
22             if (c < 0xC0) continue;    /* Skip stray trailer */
23             if (c < 0xE0) {            /* Two-byte sequense */
24                 f_read(fil, s, 1, &rc);
25                 if (rc != 1) break;
26                 c = ((c & 0x1F) << 6) | (s[0] & 0x3F);
27                 if (c < 0x80) c = '?';
28             } else {
29                 if (c < 0xF0) {        /* Three-byte sequense */
30                     f_read(fil, s, 2, &rc);
31                     if (rc != 2) break;
32                     c = (c << 12) | ((s[0] & 0x3F) << 6) | (s[1] & 0x3F);
33                     if (c < 0x800) c = '?';
34                 } else {            /* Reject four-byte sequense */
35                     c = '?';
36                 }
37             }
38         }
39 #endif
40 #if _USE_STRFUNC >= 2
41         if (c == '\r') continue;    /* Strip '\r' */
42 #endif
43         *p++ =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值