octave: Formatted Input

 
 14.2.11 Formatted Input
 -----------------------
 
 Octave provides the ‘scanf’, ‘fscanf’, and ‘sscanf’ functions to read
 formatted input.  There are two forms of each of these functions.  One
 can be used to extract vectors of data from a file, and the other is
 more ‘C-like’.
 
  -- : [VAL, COUNT, ERRMSG] = fscanf (FID, TEMPLATE, SIZE)
  -- : [V1, V2, ..., COUNT, ERRMSG] = fscanf (FID, TEMPLATE, "C")
      In the first form, read from FID according to TEMPLATE, returning
      the result in the matrix VAL.
 
      The optional argument SIZE specifies the amount of data to read and
      may be one of
 
      ‘Inf’
           Read as much as possible, returning a column vector.
 
      ‘NR’
           Read up to NR elements, returning a column vector.
 
      ‘[NR, Inf]’
           Read as much as possible, returning a matrix with NR rows.  If
           the number of elements read is not an exact multiple of NR,
           the last column is padded with zeros.
 
      ‘[NR, NC]’
           Read up to ‘NR * NC’ elements, returning a matrix with NR
           rows.  If the number of elements read is not an exact multiple
           of NR, the last column is padded with zeros.
 
      If SIZE is omitted, a value of ‘Inf’ is assumed.
 
      A string is returned if TEMPLATE specifies only character
      conversions.
 
      The number of items successfully read is returned in COUNT.
 
      If an error occurs, ERRMSG contains a system-dependent error
      message.
 
      In the second form, read from FID according to TEMPLATE, with each
      conversion specifier in TEMPLATE corresponding to a single scalar
      return value.  This form is more “C-like”, and also compatible with
      previous versions of Octave.  The number of successful conversions
      is returned in COUNT
 
      See the Formatted Input section of the GNU Octave manual for a
      complete description of the syntax of the template string.
 
DONTPRINTYET       See also: Seefgets XREFfgets, Seefgetl XREFfgetl, *noteDONTPRINTYET       See also: Seefgets XREFfgets, Seefgetl XREFfgetl, See
      fread XREFfread, Seescanf XREFscanf, Seesscanf XREFsscanf,
      Seefopen XREFfopen.
 
  -- : [VAL, COUNT, ERRMSG] = scanf (TEMPLATE, SIZE)
  -- : [V1, V2, ..., COUNT, ERRMSG] = scanf (TEMPLATE, "C")
      This is equivalent to calling ‘fscanf’ with FID = ‘stdin’.
 
      It is currently not useful to call ‘scanf’ in interactive programs.
 
DONTPRINTYET       See also: Seefscanf XREFfscanf, Seesscanf XREFsscanf, *noteDONTPRINTYET       See also: Seefscanf XREFfscanf, Seesscanf XREFsscanf, See
      printf XREFprintf.
 
  -- : [VAL, COUNT, ERRMSG, POS] = sscanf (STRING, TEMPLATE, SIZE)
  -- : [V1, V2, ..., COUNT, ERRMSG] = sscanf (STRING, TEMPLATE, "C")
      This is like ‘fscanf’, except that the characters are taken from
      the string STRING instead of from a stream.
 
      Reaching the end of the string is treated as an end-of-file
      condition.  In addition to the values returned by ‘fscanf’, the
      index of the next character to be read is returned in POS.
 
DONTPRINTYET       See also: Seefscanf XREFfscanf, Seescanf XREFscanf, *noteDONTPRINTYET       See also: Seefscanf XREFfscanf, Seescanf XREFscanf, See
      sprintf XREFsprintf.
 
    Calls to ‘scanf’ are superficially similar to calls to ‘printf’ in
 that arbitrary arguments are read under the control of a template
 string.  While the syntax of the conversion specifications in the
 template is very similar to that for ‘printf’, the interpretation of the
 template is oriented more towards free-format input and simple pattern
 matching, rather than fixed-field formatting.  For example, most ‘scanf’
 conversions skip over any amount of “white space” (including spaces,
 tabs, and newlines) in the input file, and there is no concept of
 precision for the numeric input conversions as there is for the
 corresponding output conversions.  Ordinarily, non-whitespace characters
 in the template are expected to match characters in the input stream
 exactly.
 
    When a “matching failure” occurs, ‘scanf’ returns immediately,
 leaving the first non-matching character as the next character to be
 read from the stream, and ‘scanf’ returns all the items that were
 successfully converted.
 
    The formatted input functions are not used as frequently as the
 formatted output functions.  Partly, this is because it takes some care
 to use them properly.  Another reason is that it is difficult to recover
 from a matching error.