1e6865dcdSopenharmony_ci<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 2e6865dcdSopenharmony_ci<html lang="en"> 3e6865dcdSopenharmony_ci<head> 4e6865dcdSopenharmony_ci<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5e6865dcdSopenharmony_ci<meta http-equiv="Content-Style-Type" content="text/css"> 6e6865dcdSopenharmony_ci<link rel="up" title="FatFs" href="../00index_e.html"> 7e6865dcdSopenharmony_ci<link rel="alternate" hreflang="ja" title="Japanese" href="../ja/printf.html"> 8e6865dcdSopenharmony_ci<link rel="stylesheet" href="../css_e.css" type="text/css" media="screen" title="ELM Default"> 9e6865dcdSopenharmony_ci<title>FatFs - f_printf</title> 10e6865dcdSopenharmony_ci</head> 11e6865dcdSopenharmony_ci 12e6865dcdSopenharmony_ci<body> 13e6865dcdSopenharmony_ci 14e6865dcdSopenharmony_ci<div class="para func"> 15e6865dcdSopenharmony_ci<h2>f_printf</h2> 16e6865dcdSopenharmony_ci<p>The f_printf function writes formatted string to the file.</p> 17e6865dcdSopenharmony_ci<pre> 18e6865dcdSopenharmony_ciint f_printf ( 19e6865dcdSopenharmony_ci FIL* <span class="arg">fp</span>, <span class="c">/* [IN] File object */</span> 20e6865dcdSopenharmony_ci const TCHAR* <span class="arg">fmt</span>, <span class="c">/* [IN] Format stirng */</span> 21e6865dcdSopenharmony_ci ... 22e6865dcdSopenharmony_ci); 23e6865dcdSopenharmony_ci</pre> 24e6865dcdSopenharmony_ci</div> 25e6865dcdSopenharmony_ci 26e6865dcdSopenharmony_ci<div class="para arg"> 27e6865dcdSopenharmony_ci<h4>Parameters</h4> 28e6865dcdSopenharmony_ci<dl class="par"> 29e6865dcdSopenharmony_ci<dt>fp</dt> 30e6865dcdSopenharmony_ci<dd>Pointer to the open file object structure.</dd> 31e6865dcdSopenharmony_ci<dt>fmt</dt> 32e6865dcdSopenharmony_ci<dd>Pointer to the null <tt>'\0'</tt> terminated format string. The terminator character will not be output.</dd> 33e6865dcdSopenharmony_ci<dt>...</dt> 34e6865dcdSopenharmony_ci<dd>Optional arguments...</dd> 35e6865dcdSopenharmony_ci 36e6865dcdSopenharmony_ci</dl> 37e6865dcdSopenharmony_ci</div> 38e6865dcdSopenharmony_ci 39e6865dcdSopenharmony_ci 40e6865dcdSopenharmony_ci<div class="para ret"> 41e6865dcdSopenharmony_ci<h4>Return Values</h4> 42e6865dcdSopenharmony_ci<p>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.</p> 43e6865dcdSopenharmony_ci</div> 44e6865dcdSopenharmony_ci 45e6865dcdSopenharmony_ci 46e6865dcdSopenharmony_ci<div class="para desc"> 47e6865dcdSopenharmony_ci<h4>Description</h4> 48e6865dcdSopenharmony_ci<p>The format control directive is a sub-set of standard library shown as follows:</p> 49e6865dcdSopenharmony_ci<pre> 50e6865dcdSopenharmony_ci %[flag][width][precision][size]type 51e6865dcdSopenharmony_ci</pre> 52e6865dcdSopenharmony_ci<dl> 53e6865dcdSopenharmony_ci<dt>flag</dt><dd>Padding option. A <tt>-</tt> specifies left-aligned. A <tt>0</tt> specifies zero padded. The default setting is in right-aligned and space padded.</dd> 54e6865dcdSopenharmony_ci<dt>width</dt><dd>Minimum width of the field, <tt>1-99</tt> or <tt>*</tt>. If the width of generated string is less than the minimum width, rest field is padded with spaces or zeros. An <tt>*</tt> specifies the value comes from an argument in int type. The default setting is zero.</dd> 55e6865dcdSopenharmony_ci<dt>precision</dt><dd>Specifies number of fractional digits or maximum width of string, <tt>.0-.99</tt> or <tt>.*</tt>. If the number is omitted, it is same as <tt>.0</tt>. Default setting is 6 for number and no limit for string.</dd> 56e6865dcdSopenharmony_ci<dt>size</dt><dd>Specifies size of integer argument, <tt>l</tt>(long) and <tt>ll</tt>(long long). If <tt>sizeof (long) == sizeof (int)</tt> is true (this is typical of 32-bit systems), prefix <tt>l</tt> can be omitted for long integer argument. The default size is int for integer argument and floating point argument is always assumed double as the default argument promotion.</dd> 57e6865dcdSopenharmony_ci<dt>type</dt><dd>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. 58e6865dcdSopenharmony_ci<table class="lst1"> 59e6865dcdSopenharmony_ci<tr><th>Type</th><th>Format</th><th>Argument</th><th>Length</th></tr> 60e6865dcdSopenharmony_ci<tr><td><tt>c</tt></td><td>Character</td><td rowspan="6"><tt>int</tt>,<br><tt>long</tt>,<br><tt>long long</tt></td><td>1 character.</td></tr> 61e6865dcdSopenharmony_ci<tr><td><tt>d</tt></td><td>Signed decimal</td><td>1 to 11 (20 for ll) characters.</td></tr> 62e6865dcdSopenharmony_ci<tr><td><tt>u</tt></td><td>Unsigned decimal</td><td>1 to 10 (20 for ll) characters.</td></tr> 63e6865dcdSopenharmony_ci<tr><td><tt>o</tt></td><td>Unsigned octal</td><td>1 to 11 (22 for ll) characters.</td></tr> 64e6865dcdSopenharmony_ci<tr><td><tt>x X</tt></td><td>Unsigned hexdecimal</td><td>1 to 8 (16 for ll) characters.</td></tr> 65e6865dcdSopenharmony_ci<tr><td><tt>b</tt></td><td>Unsigned binary</td><td>1 to 32 characters. Limited to lower 32 digits when ll is specified.</td></tr> 66e6865dcdSopenharmony_ci<tr><td><tt>s</tt></td><td>String</td><td><tt>TCHAR*</tt></td><td>As input string. Null pointer generates a null string.</td></tr> 67e6865dcdSopenharmony_ci<tr><td><tt>f</tt></td><td>Floating point<br>(decimal)</td><td rowspan="2"><tt>double</tt></td><td>1 to 31 characters. If the number of characters exceeds 31, it writes <tt>"±OV"</tt>. Not a number and infinite write <tt>"NaN"</tt> and <tt>"±INF"</tt>.</td></tr> 68e6865dcdSopenharmony_ci<tr><td><tt>e E</tt></td><td>Floating point<br>(e notation)</td><td>4 to 31 characters. If the number of characters exceeds 31 or exponent exceeds +99, it writes <tt>"±OV"</tt>.</td></tr> 69e6865dcdSopenharmony_ci</table> 70e6865dcdSopenharmony_ci</dd> 71e6865dcdSopenharmony_ci</dl> 72e6865dcdSopenharmony_ci<p>When FatFs is configured for Unicode API (<tt><a href="config.html#lfn_unicode">FF_LFN_UNICODE</a> >= 1</tt>), character encoding on the string fuctions, <tt>f_putc</tt>, <tt>f_puts</tt>, <tt>f_printf</tt> and <tt>f_gets</tt> 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 <em>on the file</em> to be written via this function is selected by <tt><a href="config.html#strf_encode">FF_STRF_ENCODE</a></tt>. The characters with wrong encoding or invalid for the output encoding will be lost.</p> 73e6865dcdSopenharmony_ci<p>If <tt>sprintf</tt> is used in the project and code conversion is not needed, <tt>f_write</tt> with <tt>sprintf</tt> will be efficient in code size and performance rather than <tt>f_printf</tt>.</p> 74e6865dcdSopenharmony_ci</div> 75e6865dcdSopenharmony_ci 76e6865dcdSopenharmony_ci 77e6865dcdSopenharmony_ci<div class="para comp"> 78e6865dcdSopenharmony_ci<h4>QuickInfo</h4> 79e6865dcdSopenharmony_ci<p>This is a wrapper function of <a href="write.html"><tt>f_write</tt></a> function. Available when <tt><a href="config.html#fs_readonly">FF_FS_READONLY</a> == 0</tt> and <tt><a href="config.html#use_strfunc">FF_USE_STRFUNC</a> >= 1</tt>. When <tt>FF_USE_STRFUNC == 2</tt>, <tt>'\n'</tt>s in the generated string are written as <tt>'\r'+'\n'</tt> each.</p> 80e6865dcdSopenharmony_ci</div> 81e6865dcdSopenharmony_ci 82e6865dcdSopenharmony_ci 83e6865dcdSopenharmony_ci<div class="para use"> 84e6865dcdSopenharmony_ci<h4>Example</h4> 85e6865dcdSopenharmony_ci<pre> 86e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%d", 1234); <span class="c">/* "1234" */</span> 87e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%6d,%3d%%", -200, 5); <span class="c">/* " -200, 5%" */</span> 88e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%-6u", 100); <span class="c">/* "100 " */</span> 89e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%ld", 12345678); <span class="c">/* "12345678" */</span> 90e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%llu", 0x100000000); <span class="c">/* "4294967296" (<a href="config.html#print_lli">FF_PRINT_LLI</a>) */</span> 91e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%lld", -1LL); <span class="c">/* "-1" (FF_PRINT_LLI) */</span> 92e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%04x", 0xA3); <span class="c">/* "00a3" */</span> 93e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%08lX", 0x123ABC); <span class="c">/* "00123ABC" */</span> 94e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%016b", 0x550F); <span class="c">/* "0101010100001111" */</span> 95e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%*d", 6, 100); <span class="c">/* " 100" */</span> 96e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%s", "abcdefg"); <span class="c">/* "abcdefg" */</span> 97e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%5s", "abc"); <span class="c">/* " abc" */</span> 98e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%-5s", "abc"); <span class="c">/* "abc " */</span> 99e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%.5s", "abcdefg"); <span class="c">/* "abcde" */</span> 100e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%-5.2s", "abcdefg"); <span class="c">/* "ab " */</span> 101e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%c", 'a'); <span class="c">/* "a" */</span> 102e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%12f", 10.0); <span class="c">/* " 10.000000" (<a href="config.html#print_fp">FF_PRINT_FLOAT</a>) */</span> 103e6865dcdSopenharmony_ci <em>f_printf</em>(fp, "%.4E", 123.45678); <span class="c">/* "1.2346E+02" (FF_PRINT_FLOAT) */</span> 104e6865dcdSopenharmony_ci</pre> 105e6865dcdSopenharmony_ci</div> 106e6865dcdSopenharmony_ci 107e6865dcdSopenharmony_ci 108e6865dcdSopenharmony_ci<div class="para ref"> 109e6865dcdSopenharmony_ci<h4>See Also</h4> 110e6865dcdSopenharmony_ci<p><tt><a href="open.html">f_open</a>, <a href="putc.html">f_putc</a>, <a href="puts.html">f_puts</a>, <a href="gets.html">f_gets</a>, <a href="close.html">f_close</a>, <a href="sfile.html">FIL</a></tt></p> 111e6865dcdSopenharmony_ci</div> 112e6865dcdSopenharmony_ci 113e6865dcdSopenharmony_ci<p class="foot"><a href="../00index_e.html">Return</a></p> 114e6865dcdSopenharmony_ci</body> 115e6865dcdSopenharmony_ci</html> 116