Next: Overloading, Previous: Return values, Up: Methods [Index]
C functions like scanf(3), sscanf(3), and fscanf(3) have templates that allow you to call them with a variable number of arguments.
If you need to call other C functions that use variable arguments,
you must call __ctalkLibcFnWithMethodVarArgs
with the
name of the function, the method that contains the function’s
template, and the function’s return class.
The readFormat
method (implemented in String
and
ReadFileStream
classes) can scan a string or input file into
the objects that the program gives as arguments. The methods also
take care of scalar-to-object translation, memory allocation, and
several other tasks.
However, programs can also accomplish the same thing manually.
For example, here is the code of the template method for
fscanf(3), cFscanf
(CFunction
class), without the
preprocessing directives.
cFscanf (FILE *s, char *fmt,...) { EXPR_PARSER *parser; OBJECT *result_object; parser = __ctalkGetExprParserAt (__ctalkGetExprParserPtr ()); result_object = __ctalkLibcFnWithMethodVarArgs ((int (*)())fscanf, parser -> e_method, "Integer"); return result_object; }
At run time, the e_method
member of an expression parser
contains the method and its arguments.
The third argument of __ctalkLibcFnWithMethodVarArgs
determines
the class of result_object.
For C library functions that use
variable arguments, the return class is Integer.
The typecast (int (*)())
in the first argument in front of
fscanf
is not strictly needed because we know that the number
and types of arguments to fscanf(3) (or scanf(3) or any
other variable-argument function) might vary from the
__ctalkLibcFnWithMethodVarArgs
prototype, but it tells the
compiler not to print a warning message in that case.
If you simply need to print formatted output, the writeFormat
or
printOn
methods (implemented in String
,
WriteFileStream
and many other classes classes) perform
format-argument translations automatically. Several classes also
implement a readFmt
method, which reads formatted input from a
String
or ReadFileStream
. See writeFormat--class WriteFileStream, and writeFormat--class String.
Next: Overloading, Previous: Return values, Up: Methods [Index]