The f_printf function writes formatted string to the file.
int f_printf ( FIL* fp, /* [IN] File object */ const TCHAR* fmt, /* [IN] Format stirng */ ... );
When the string was written successfuly, it returns number of character encoding units written to the file. When the function failed due to disk full or an error, a negative value will be returned.
The format control directive is a sub-set of standard library shown as follows:
%[flag][width][precision][size]type
Type | Format | Argument | Length |
---|---|---|---|
c | Character | int, long, long long | 1 character. |
d | Signed decimal | 1 to 11 (20 for ll) characters. | |
u | Unsigned decimal | 1 to 10 (20 for ll) characters. | |
o | Unsigned octal | 1 to 11 (22 for ll) characters. | |
x X | Unsigned hexdecimal | 1 to 8 (16 for ll) characters. | |
b | Unsigned binary | 1 to 32 characters. Limited to lower 32 digits when ll is specified. | |
s | String | TCHAR* | As input string. Null pointer generates a null string. |
f | Floating point (decimal) | double | 1 to 31 characters. If the number of characters exceeds 31, it writes "±OV". Not a number and infinite write "NaN" and "±INF". |
e E | Floating point (e notation) | 4 to 31 characters. If the number of characters exceeds 31 or exponent exceeds +99, it writes "±OV". |
When FatFs is configured for Unicode API (FF_LFN_UNICODE >= 1), character encoding on the string fuctions, f_putc, f_puts, f_printf and f_gets function, is also switched to Unicode. The Unicode characters in multiple encoding unit, such as surrogate pair and multi-byte sequence, should not be divided into two function calls, or the character will be lost. The character encoding on the file to be written via this function is selected by FF_STRF_ENCODE. The characters with wrong encoding or invalid for the output encoding will be lost.
If sprintf is used in the project and code conversion is not needed, f_write with sprintf will be efficient in code size and performance rather than f_printf.
This is a wrapper function of f_write function. Available when FF_FS_READONLY == 0 and FF_USE_STRFUNC >= 1. When FF_USE_STRFUNC == 2, '\n's in the generated string are written as '\r'+'\n' each.
f_printf(fp, "%d", 1234); /* "1234" */ f_printf(fp, "%6d,%3d%%", -200, 5); /* " -200, 5%" */ f_printf(fp, "%-6u", 100); /* "100 " */ f_printf(fp, "%ld", 12345678); /* "12345678" */ f_printf(fp, "%llu", 0x100000000); /* "4294967296" (FF_PRINT_LLI) */ f_printf(fp, "%lld", -1LL); /* "-1" (FF_PRINT_LLI) */ f_printf(fp, "%04x", 0xA3); /* "00a3" */ f_printf(fp, "%08lX", 0x123ABC); /* "00123ABC" */ f_printf(fp, "%016b", 0x550F); /* "0101010100001111" */ f_printf(fp, "%*d", 6, 100); /* " 100" */ f_printf(fp, "%s", "abcdefg"); /* "abcdefg" */ f_printf(fp, "%5s", "abc"); /* " abc" */ f_printf(fp, "%-5s", "abc"); /* "abc " */ f_printf(fp, "%.5s", "abcdefg"); /* "abcde" */ f_printf(fp, "%-5.2s", "abcdefg"); /* "ab " */ f_printf(fp, "%c", 'a'); /* "a" */ f_printf(fp, "%12f", 10.0); /* " 10.000000" (FF_PRINT_FLOAT) */ f_printf(fp, "%.4E", 123.45678); /* "1.2346E+02" (FF_PRINT_FLOAT) */