Product Documentation
Cadence SKILL Language Reference
Product Version IC23.1, November 2023

fscanf, scanf, sscanf

fscanf( 
p_inputPort 
t_formatString 
[ s_var1 ... ] 
) 
=> x_items / nil

scanf( 
t_formatString 
[ s_var1 ... ] 
) 
=> x_items / nil

sscanf( 
t_sourceString 
t_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.

Common Input Format Specifications
Format Specification Type(s) of Argument Scans for

%d

fixnum

An integer

%f

flonum

A floating-point number

%s

string

A string (delimited by spaces) in the input

Arguments

p_inputPort

Input port fscanf reads from. The input port cannot be the CIW for fscanf.

t_sourceString

Input string for sscanf.

t_formatString

Format string to match against in the reading.

s_var1

Name of variable to store results of read.

Value Returned

x_items

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.

nil

It encounters an end of file.

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

fprintf

lineread


Return to top
 ⠀
X