fscanf, scanf, sscanf
fscanf(p_inputPortt_formatString[s_var1... ] ) =>x_items/ nil scanf(t_formatString[s_var1... ] ) =>x_items/ nil sscanf(t_sourceStringt_formatString[s_var1... ] ) =>x_items/ nil
Description
The main difference between these functions is the source of input. fscanf reads input from a port according to format specifications and returns the number of items read in. scanf takes its input from piport implicitly. scanf only works in standalone SKILL when the piport is not the CIW. sscanf reads its input from a string instead of a port. Another difference is that whereas sscanf supports the width while reading floating-point numbers from the input string, fscanf and scanf do not.
The results are stored into corresponding variables in the call. The fscanf function can be considered the inverse function of the fprintf output function. The fscanf function returns the number of input items it successfully matched with its format string. It returns nil if it encounters an end of file.
The maximum size of any input string being read as a string variable for fscanf is currently limited to 8K. Also, the function lineread is a faster alternative to fscanf for reading SKILL objects.
If an error is found while scanning for input, only those variables read before the error will be assigned.
The common input formats accepted by fscanf are summarized below.
| Format Specification | Type(s) of Argument | Scans for |
|---|---|---|
Arguments
|
Input port |
|
Value Returned
|
The number of input items it successfully read in. As a side-effect, the items read in are assigned to the corresponding variables specified in the call. |
|
Examples
Scans for an integer and a floating-point number from the input port p and stores the values read in the variables i and d, respectively.
fscanf( p "%d %f" i d )
Assume a file testcase with one line:
hello 2 3 world
x = infile("testcase")
=> port:"testcase"
fscanf( x "%s %d %d %s" a b c d )
=> 4
(list a b c d)
=> ("hello" 2 3 "world")
Scans the given floating point number as val1 (1.23) and val2 (4) and returns the resulting number as 2 because two values were read.
s = "1.234"
sscanf(s "%4f%d" val1 val2)
Related Topics
Return to top