Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ Language Reference - fscanf http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
   fscanf

   Usage
   #include <stdio.h>
   int fscanf(FILE *fp,const char *format,...);

   Description
   Reads  characters  from  the  input stream  fp.  Characters  read  are
   converted  according to the format string and the values  created  are
   stored through the argument pointers.

   Note that the arguments are pointers to where values are to be stored.
   The format string consists of:
   1.    Spaces, tabs and newlines which cause input to be skipped up  to
   the next character which is not a whitespace.
   2.    Other  characters, except for % which are matched  against  the
   input.
   3.    A conversion specification, which is as follows:

   '%' ['*'] [field-width] [precision] conv_char
   This  specifies how input characters are to be converted and  assigned
   through  the  corresponding argument  pointers.  Conversion  continues
   until a conflicting input character is encountered or the  field_width
   is reached.

   *
   is  an  assignment suppression flag. It causes the  conversion  to  be
   performed  but  the  result  is ignored.  There  is  no  corresponding
   argument pointer for this.

   If there are less argument pointers than conversion specification  the
   results  are unpredictable. If there are extra argument  pointers  the
   excess ones are ignored.

   field_width  is  a sequence of decimal digits specifying  the  maximum
   number of characters in the field.

   precision
   L, l
   If  l  is  used  with one of the (b, d, i,  o,  u,  or  x)  conversion
   characters, then it indicates that the argument is a pointer to a long
   rather than a pointer to an int. The l or L flag, when used with the e
   or  f conversion character, means that the argument is a pointer to  a
   double rather than a pointer to a float.

   h
   Argument is a pointer to a short.
   Conversion Characters: conv_char

   The conversion characters are:

   b
   A binary number is expected, the argument pointer must be a pointer to
   an int.

   d
   An  integer is expected, the argument pointer must be a pointer to  an
   int.

   e, E, f, g, G
   A  floating point number is expected. The format of the number is  the
   same  as  in  C. The argument pointer must be a  pointer  to  a  float
   (double if l or L is used).

   i
   An  integer  is  expected. If it starts with a 0, it is  taken  to  be
   octal.  If  it starts with 0x or 0X, it is hexadecimal.  The  argument
   pointer must be a pointer to an int.

   o
   An octal number is expected. The argument pointer must be a pointer to
   an int.

   p
   A  hexadecimal  integer is expected. The argument pointer  must  be  a
   pointer to a pointer.

   u
   An  unsigned  integer  is expected, the argument  pointer  must  be  a
   pointer to an unsigned.

   n
   Through  the  argument  pointer is stored an  integer  specifying  the
   number of characters read up to this point by this call to fscanf.

   [
   A  string  is  expected. Between the [ and a closing  ]  will  be  the
   characters acceptable to the string. If the [ is immediately  followed
   by a , the acceptable characters for the string are all those  except
   the  ones  between the  and the ] . The argument pointer  must  be  a
   pointer to a string. A 0 is appended to the string.

   s
   A  string  is expected. The argument pointer must be a  pointer  to  a
   string.  The  input field extends until a space or  newline  is  read,
   which is not part of the field. A 0 is appended to the string.

   c
   A  character is expected. The argument pointer must be a pointer to  a
   character.  If a field_width is specified, then that  many  characters
   are  read  and the argument pointer must point to  a  character  array
   large enough to hold the result.

   %
   Match the input with a %.

   The  conversion  characters  e, g and x may be in  capitals,  with  no
   difference   in  meaning.  Other  conversion  characters  will   cause
   unexpected results.

   Conflicting  characters are left unread in the input stream. There  is
   no  direct  way  to determine if  suppressed  assignments  or  literal
   matches succeeded, unless %n is used.

   Example
   #include <stdio.h>
   char fst[10], *lst = "     ";
   int ttl;
   main()
   {
   int rt;
        rt = fscanf(stdin, "%s %s %d",fst,lst,&ttl);
        printf("Values read %s %s %d\n",fst,lst,ttl);
        return rt = 3;
   }

   Return Value
   The number of assigned input items excluding any assignment suppressed
   conversions is returned. If the end of file is encountered before  any
   assignments  are  done  or  before any  conflicts  occur,  an  EOF  is
   returned.  fscanf()  normally returns when it reaches the end  of  the
   format string.


See Also: printf scanf sscanf

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson