码迷,mamicode.com
首页 > 其他好文 > 详细

FATFS 初学之 f_gets/ f_putc/ f_puts/ f_printf

时间:2014-08-14 14:02:38      阅读:2074      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   os   io   文件   

详见:嵌入式大讲堂

 

f_gets:

bubuko.com,布布扣
 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++ = c;
44         n++;
45         if (c == \n) break;        /* Break on EOL */
46     }
47     *p = 0;
48     return n ? buff : 0;            /* When no data read (eof or error), return with error. */
49 }
View Code

函数功能:f_gets从文件中读取一个字符串。

描述:

f_gets函数当_USE_STRFUNC == 1或者_USE_STRFUNC == 2时可用。如果_USE_STRFUNC == 2,文件中包含的‘\r‘则被去除。
f_gets函数是f_read的一个封装函数。当读取到‘\n‘、文件结束或缓冲区被填冲了Size - 1个字符时,读操作结束。读取的字符串以‘\0‘结束。当文件结束或读操作中发生了任何错误,f_gets()返回一个空字符串。可以使用宏f_eof()和f_error()检查EOF和错误状态。

 

f_putc:

bubuko.com,布布扣
 1 /*-----------------------------------------------------------------------*/
 2 /* Put a character to the file                                           */
 3 /*-----------------------------------------------------------------------*/
 4 int f_putc (
 5     TCHAR c,    /* A character to be output */
 6     FIL* fil    /* Pointer to the file object */
 7 )
 8 {
 9     UINT bw, btw;
10     BYTE s[3];
11 
12 
13 #if _USE_STRFUNC >= 2
14     if (c == \n) f_putc (\r, fil);    /* LF -> CRLF conversion */
15 #endif
16 
17 #if _LFN_UNICODE    /* Write the character in UTF-8 encoding */
18     if (c < 0x80) {            /* 7-bit */
19         s[0] = (BYTE)c;
20         btw = 1;
21     } else {
22         if (c < 0x800) {    /* 11-bit */
23             s[0] = (BYTE)(0xC0 | (c >> 6));
24             s[1] = (BYTE)(0x80 | (c & 0x3F));
25             btw = 2;
26         } else {            /* 16-bit */
27             s[0] = (BYTE)(0xE0 | (c >> 12));
28             s[1] = (BYTE)(0x80 | ((c >> 6) & 0x3F));
29             s[2] = (BYTE)(0x80 | (c & 0x3F));
30             btw = 3;
31         }
32     }
33 #else                /* Write the character without conversion */
34     s[0] = (BYTE)c;
35     btw = 1;
36 #endif
37     f_write(fil, s, btw, &bw);        /* Write the char to the file */
38     return (bw == btw) ? 1 : EOF;    /* Return the result */
39 }
View Code

函数功能:f_putc函数向文件中写入一个字符。

描述:

f_putc函数当(_FS_READONLY == 0)&&(_USE_STRFUNC == 1 || _USE_STRFUNC == 2)时可用。当_USE_STRFUNC == 2时,字符‘\n‘被转换为"\r\n"写入文件中。
f_putc函数是f_write的一个封装函数。

 

f_puts:

bubuko.com,布布扣
 1 /*-----------------------------------------------------------------------*/
 2 /* Put a string to the file                                              */
 3 /*-----------------------------------------------------------------------*/
 4 int f_puts (
 5     const TCHAR* str,    /* Pointer to the string to be output */
 6     FIL* fil            /* Pointer to the file object */
 7 )
 8 {
 9     int n;
10 
11 
12     for (n = 0; *str; str++, n++) {
13         if (f_putc(*str, fil) == EOF) return EOF;
14     }
15     return n;
16 }
View Code

函数功能:f_puts函数向文件中写入一个字符串。

描述:

f_puts()当(_FS_READONLY == 0)&&(_USE_STRFUNC == 1 || _USE_STRFUNC == 2)时可用。当_USE_STRFUNC == 2时,字符串中的‘\n‘被转换为"\r\n"写入文件中。
f_puts()是f_putc()的一个封装函数。

 

f_printf:

bubuko.com,布布扣
 1 /*-----------------------------------------------------------------------*/
 2 /* Put a formatted string to the file                                    */
 3 /*-----------------------------------------------------------------------*/
 4 int f_printf (
 5     FIL* fil,            /* Pointer to the file object */
 6     const TCHAR* str,    /* Pointer to the format string */
 7     ...                    /* Optional arguments... */
 8 )
 9 {
10     va_list arp;
11     BYTE f, r;
12     UINT i, j, w;
13     ULONG v;
14     TCHAR c, d, s[16], *p;
15     int res, chc, cc;
16 
17 
18     va_start(arp, str);
19 
20     for (cc = res = 0; cc != EOF; res += cc) {
21         c = *str++;
22         if (c == 0) break;            /* End of string */
23         if (c != %) {                /* Non escape character */
24             cc = f_putc(c, fil);
25             if (cc != EOF) cc = 1;
26             continue;
27         }
28         w = f = 0;
29         c = *str++;
30         if (c == 0) {                /* Flag: ‘0‘ padding */
31             f = 1; c = *str++;
32         } else {
33             if (c == -) {            /* Flag: left justified */
34                 f = 2; c = *str++;
35             }
36         }
37         while (IsDigit(c)) {        /* Precision */
38             w = w * 10 + c - 0;
39             c = *str++;
40         }
41         if (c == l || c == L) {    /* Prefix: Size is long int */
42             f |= 4; c = *str++;
43         }
44         if (!c) break;
45         d = c;
46         if (IsLower(d)) d -= 0x20;
47         switch (d) {                /* Type is... */
48         case S :                    /* String */
49             p = va_arg(arp, TCHAR*);
50             for (j = 0; p[j]; j++) ;
51             chc = 0;
52             if (!(f & 2)) {
53                 while (j++ < w) chc += (cc = f_putc( , fil));
54             }
55             chc += (cc = f_puts(p, fil));
56             while (j++ < w) chc += (cc = f_putc( , fil));
57             if (cc != EOF) cc = chc;
58             continue;
59         case C :                    /* Character */
60             cc = f_putc((TCHAR)va_arg(arp, int), fil); continue;
61         case B :                    /* Binary */
62             r = 2; break;
63         case O :                    /* Octal */
64             r = 8; break;
65         case D :                    /* Signed decimal */
66         case U :                    /* Unsigned decimal */
67             r = 10; break;
68         case X :                    /* Hexdecimal */
69             r = 16; break;
70         default:                    /* Unknown type (passthrough) */
71             cc = f_putc(c, fil); continue;
72         }
73 
74         /* Get an argument and put it in numeral */
75         v = (f & 4) ? (ULONG)va_arg(arp, long) : ((d == D) ? (ULONG)(long)va_arg(arp, int) : (ULONG)va_arg(arp, unsigned int));
76         if (d == D && (v & 0x80000000)) {
77             v = 0 - v;
78             f |= 8;
79         }
80         i = 0;
81         do {
82             d = (TCHAR)(v % r); v /= r;
83             if (d > 9) d += (c == x) ? 0x27 : 0x07;
84             s[i++] = d + 0;
85         } while (v && i < sizeof(s) / sizeof(s[0]));
86         if (f & 8) s[i++] = -;
87         j = i; d = (f & 1) ? 0 :  ;
88         res = 0;
89         while (!(f & 2) && j++ < w) res += (cc = f_putc(d, fil));
90         do res += (cc = f_putc(s[--i], fil)); while(i);
91         while (j++ < w) res += (cc = f_putc( , fil));
92         if (cc != EOF) cc = res;
93     }
94 
95     va_end(arp);
96     return (cc == EOF) ? cc : res;
97 }
View Code

函数功能:f_printf函数向文件中写入一个格式化字符串。

描述:

f_printf函数当(_FS_READONLY == 0)&&(_USE_STRFUNC == 1 || _USE_STRFUNC == 2)时可用。当_USE_STRFUNC == 2时,包含在格式化字符串中的‘\n‘将被转换成"\r\n"写入文件中。

f_printf函数是f_putc和f_puts的一个封装函数。

 

例:

bubuko.com,布布扣
 1 f_gets(buf, NUM, &fil);            // 从文件中读取一个字符串的前 NUM个字符
 2 f_putc(ch, &fil);                // 向文件中写入一个字符
 3 f_puts((char *)buffer, &fil);    // 向文件内写入一个字符串
 4 
 5 f_printf(&fil, "%6d", -200);         /* "  -200" */
 6 f_printf(&fil, "%02u", 5);           /* "05" */
 7 f_printf(&fil, "%ld", 12345678L);    /* "12345678" */
 8 f_printf(&fil, "%08lX", 1194684UL);  /* "00123ABC" */
 9 f_printf(&fil, "%s", "String");      /* "String" */
10 f_printf(&fil, "%c", a);           /* "a" */
View Code

 

FATFS 初学之 f_gets/ f_putc/ f_puts/ f_printf,布布扣,bubuko.com

FATFS 初学之 f_gets/ f_putc/ f_puts/ f_printf

标签:style   blog   http   color   使用   os   io   文件   

原文地址:http://www.cnblogs.com/Danhuise/p/3912296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!