f_printf

The f_printf function writes formatted string to the file.

int f_printf (
  FIL* fp,          /* [IN] File object */
  const TCHAR* fmt, /* [IN] Format stirng */
  ...
);

Parameters

fp
Pointer to the open file object structure.
fmt
Pointer to the null '\0' terminated format string. The terminator character will not be output.
...
Optional arguments...

Return Values

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.

Description

The format control directive is a sub-set of standard library shown as follows:

    %[flag][width][precision][size]type
flag
Padding options. A - specifies left-aligned. A 0 specifies zero padded. The default setting is in right-aligned and space padded.
width
Minimum width of the field, 1-99 or *. If the width of generated string is less than the specified value, rest field is padded with spaces or zeros. An * specifies the value comes from an argument in int type.
precision
Specifies number of fractional digits or maximum width of string, .0-.99 or .*. If number is omitted, it will be same as .0. Default setting is 6 for number and no limit for string.
size
Specifies size of integer argument, l(long) and ll(long long). If sizeof (long) == sizeof (int) is true (this is typical of 32-bit systems), prefix l can be omitted for long integer argument. The default size is int for integer arrument and floating point argument is always assumed double.
type
Specifies type of the output format and the argument as shown below. The length of generated string is in assumtion of int is 32-bit.
TypeFormatArgumentLength
cCharacterint,
long,
long long
1 character.
dSigned decimal1 to 11 (20 for ll) characters.
uUnsigned decimal1 to 10 (20 for ll) characters.
oUnsigned octal1 to 11 (22 for ll) characters.
x XUnsigned hexdecimal1 to 8 (16 for ll) characters.
bUnsigned binary1 to 32 characters. Limited to lower 32 digits when ll is specified.
sStringTCHAR*As input string. Null pointer generates a null string.
fFloating point
(decimal)
double1 to 31 characters. If the number of characters exceeds 31, it writes "±OV". Not a number and infinite write "NaN" and "±INF".
e EFloating 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.

QuickInfo

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.

Example

    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) */

See Also

f_open, f_putc, f_puts, f_gets, f_close, FIL

Return