详见:嵌入式大讲堂
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++ =