fscanf
Input format conversion
Interface
#include <stdio.h>
int | scanf (const char * restrict format, ...) |
int | fscanf (FILE * restrict stream, const char * restrict format, ...) |
int | sscanf (const char * restrict str, const char * restrict format, ...) |
#include <stdarg.h>
int | vscanf (const char * restrict format, va_list ap) |
int | vsscanf (const char * restrict str, const char * restrict format, va_list ap) |
int | vfscanf (FILE * restrict stream, const char * restrict format, va_list ap) |
Description
The scanf family of functions scans input according to aformat
as described below. This format may contain <span class="Em">conversion</span> <span class="Em">specifiers</span> the results from such conversions, if any, are stored through the <span class="Em">pointer</span> arguments. The scanf function reads input from the standard input stream <span class="Dv">stdin</span>, fscanf reads input from the stream pointer stream
, and sscanf reads its input from the character string pointed to by str
. The vfscanf function is analogous to vfprintf and reads input from the stream pointer stream
using a variable argument list of pointers (see stdarg). The vscanf function scans a variable argument list from the standard input and the vsscanf function scans it from a string; these are analogous to the vprintf and vsprintf functions respectively. Each successive <span class="Em">pointer</span> argument must correspond properly with each successive conversion specifier (but see the <span class="Cm">*</span> and <span class="Cm">%n </span> conversions below). All conversions are introduced by the <span class="Cm">%</span> (percent sign) character or <span class="Cm">%n </span> sequence. #include <the> <span class="Em">pointer</span> will be the <span class="Cm">n</span> th argument after the format string. The format
string may also contain other characters. White space (such as blanks, tabs, or newlines) in the format
string match any amount of white space, including none, in the input. Everything else matches only itself. Scanning stops when an input character does not match such a format character. Scanning also stops when an input conversion cannot be made (see below).
Extended locale versions of these functions are documented in scanf_l. See xlocale for more information.
The code below attempts to open the text file "fred.txt" in the current directory and read each
character in it until the EOF symbol is encountered. Reading from the file is achieved using the fscanf function.Example 1
#include <stdio.h> int main() { FILE *in; if (in = fopen("fred.txt", "rt")) { for (char c; !feof(in); fscanf(in, "%c", &c)); fclose(in); } return 0; }
Conversions
Following the <span class="Cm">%</span> character or <span class="Cm">%n </span> sequence introducing a conversion there may be a number of <span class="Em">flag</span> characters, as follows: <table cellspacing="0" class="refpage" style="margin-left:25px"> <tr> <td valign="top" nowrap> <span class="Cm">*</span> </td> <td valign="top"> Suppresses assignment. The conversion that follows occurs as usual, but no pointer is used; the result of the conversion is simply discarded. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">hh</span> </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">char</span> (rather than <span class="Vt">int</span>). </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">h</span> </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">short int</span> (rather than <span class="Vt">int</span>). </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">l</span> (ell) </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">long int</span> (rather than <span class="Vt">int</span>), that the conversion will be one of <span class="Cm">a</span>, <span class="Cm">e</span>, <span class="Cm">f</span>, or <span class="Cm">g</span> and the next pointer is a pointer to <span class="Vt">double</span> (rather than <span class="Vt">float</span>), or that the conversion will be one of <span class="Cm">c</span>, <span class="Cm">s</span> or <span class="Cm">[</span> and the next pointer is a pointer to an array of <span class="Vt">wchar_t</span> (rather than <span class="Vt">char</span>). </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">ll</span> (ell ell) </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">long long int</span> (rather than <span class="Vt">int</span>). </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">L</span> </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">a</span>, <span class="Cm">e</span>, <span class="Cm">f</span>, or <span class="Cm">g</span> and the next pointer is a pointer to <span class="Vt">long double</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">j</span> </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">intmax_t</span> (rather than <span class="Vt">int</span>). </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">t</span> </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">ptrdiff_t</span> (rather than <span class="Vt">int</span>). </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">z</span> </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">size_t</span> (rather than <span class="Vt">int</span>). </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">q</span> </td> <td valign="top"> (deprecated.) Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">long long int</span> (rather than <span class="Vt">int</span>). </td> </tr> </table> \n \n In addition to these flags, there may be an optional maximum field width, expressed as a decimal integer, between the <span class="Cm">%</span> and the conversion. If no width is given, a default of "<span class="Dq">infinity</span>" is used (with one exception, below); otherwise at most this many bytes are scanned in processing the conversion. In the case of the <span class="Cm">lc</span>, <span class="Cm">ls</span> and <span class="Cm">l[</span> conversions, the field width specifies the maximum number of multibyte characters that will be scanned. Before conversion begins, most conversions skip white space; this white space is not counted against the field width. The following conversions are available: <table cellspacing="0" class="refpage" style="margin-left:25px"> <tr> <td valign="top" nowrap> <span class="Cm">%</span> </td> <td valign="top"> Matches a literal '<span class="Qlq">%</span>'. That is, "<span class="Dq"><span class="Li">%%</span></span>" in the format string matches a single input '<span class="Qlq">%</span>' character. No conversion is done, and assignment does not occur. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">d</span> </td> <td valign="top"> Matches an optionally signed decimal integer; the next pointer must be a pointer to <span class="Vt">int</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">i</span> </td> <td valign="top"> Matches an optionally signed integer; the next pointer must be a pointer to <span class="Vt">int</span>. The integer is read in base 16 if it begins with '<span class="Qlq">0x</span>' or '<span class="Qlq">0X</span>', in base 8 if it begins with '<span class="Qlq">0</span>', and in base 10 otherwise. Only characters that correspond to the base are used. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">o</span> </td> <td valign="top"> Matches an octal integer; the next pointer must be a pointer to <span class="Vt">unsigned int</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">u</span> </td> <td valign="top"> Matches an optionally signed decimal integer; the next pointer must be a pointer to <span class="Vt">unsigned int</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">x</span>, <span class="Cm">X</span> </td> <td valign="top"> Matches an optionally signed hexadecimal integer; the next pointer must be a pointer to <span class="Vt">unsigned int</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">a</span>, <span class="Cm">A</span>, <span class="Cm">e</span>, <span class="Cm">E</span>, <span class="Cm">f</span>, <span class="Cm">F</span>, <span class="Cm">g</span>, <span class="Cm">G</span> </td> <td valign="top"> Matches a floating-point number in the style of reference:strtod. The next pointer must be a pointer to <span class="Vt">float</span> (unless <span class="Cm">l</span> or <span class="Cm">L</span> is specified.) </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">s</span> </td> <td valign="top"> Matches a sequence of non-white-space characters; the next pointer must be a pointer to <span class="Vt">char</span>, and the array must be large enough to accept all the sequence and the terminating <span class="Dv">NUL</span> character. The input string stops at white space or at the maximum field width, whichever occurs first. \n \n If an <span class="Cm">l</span> qualifier is present, the next pointer must be a pointer to <span class="Vt">wchar_t</span>, into which the input will be placed after conversion by mbrtowc. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">S</span> </td> <td valign="top"> The same as <span class="Cm">ls</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">c</span> </td> <td valign="top"> Matches a sequence of <span class="Em">width</span> count characters (default 1); the next pointer must be a pointer to <span class="Vt">char</span>, and there must be enough room for all the characters (no terminating <span class="Dv">NUL</span> is added). The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format. \n \n If an <span class="Cm">l</span> qualifier is present, the next pointer must be a pointer to <span class="Vt">wchar_t</span>, into which the input will be placed after conversion by mbrtowc. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">C</span> </td> <td valign="top"> The same as <span class="Cm">lc</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">[</span> </td> <td valign="top"> Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to <span class="Vt">char</span>, and there must be enough room for all the characters in the string, plus a terminating <span class="Dv">NUL</span> character. The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined by the characters between the open bracket <span class="Cm">[</span> character and a close bracket <span class="Cm">]</span> character. The set <span class="Em">excludes</span> those characters if the first character after the open bracket is a circumflex <span class="Cm">^</span>. To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. The hyphen character <span class="Cm">-</span> is also special; when placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, '<span class="Qlq">[^]0-9-]</span>' means the set "<span class="Dq">everything except close bracket, zero through nine, and hyphen</span>". The string ends with the appearance of a character not in the (or, with a circumflex, in) set or when the field width runs out. If an <span class="Cm">l</span> qualifier is present, the next pointer must be a pointer to <span class="Vt">wchar_t</span>, into which the input will be placed after conversion by mbrtowc. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">p</span> </td> <td valign="top"> Matches a pointer value (as printed by '<span class="Qlq">%p</span>' in reference:printf); the next pointer must be a pointer to <span class="Vt">void</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">n</span> </td> <td valign="top"> Nothing is expected; instead, the number of characters consumed thus far from the input is stored through the next pointer, which must be a pointer to <span class="Vt">int</span>. This is <span class="Em">not</span> a conversion, although it can be suppressed with the <span class="Cm">*</span> flag. </td> </tr> </table> The decimal point character is defined in the program's locale (category <span class="Dv">LC_NUMERIC</span>). For backwards compatibility, a "<span class="Dq">conversion</span>" of '<span class="Qlq">%\0</span>' causes an immediate return of <span class="Dv">EOF</span>.Return Values
These functions return the number of input items assigned, which can be fewer than provided for, or even zero, in the event of a matching failure. Zero indicates that, while there was input available, no conversions were assigned; typically this is due to an invalid input character, such as an alphabetic character for a '<span class="Qlq">%d</span>' conversion. The value <span class="Dv">EOF</span> is returned if an input failure occurs before any conversion such as an end-of-file occurs. If an error or end-of-file occurs after conversion has begun, the number of conversions which were successfully completed is returned.Bugs
Earlier implementations of scanf treated <span class="Cm">%D</span>, <span class="Cm">%E</span>, <span class="Cm">%F</span>, <span class="Cm">%O</span> and <span class="Cm">%X</span> as their lowercase equivalents with an <span class="Cm">l</span> modifier. In addition, scanf treated an unknown conversion character as <span class="Cm">%d</span> or <span class="Cm">%D</span>, depending on its case. This functionality has been removed. Numerical strings are truncated to 512 characters; for example, <span class="Cm">%f</span> and <span class="Cm">%d</span> are implicitly <span class="Cm">%512f</span> and <span class="Cm">%512d</span>. The scanf family of functions do not correctly handle multibyte characters in theformat
argument.