Format Characters for Methods That Scan
This topic describes format characters for methods that scan. The following methods can perform a scan operation:
Note the following:
The format string includes character combinations that specify the type of data.
The format string specifies input sequences and how the method must convert the input.
The method maps each character to the input as it reads the input until it encounters a percentage symbol (%).
The percentage symbol causes the method to read the value, and then store it in an argument that follows the format string.
Each argument that occurs after the format string receives the next parsed value from the next argument in the list of arguments that occur after the format string.
Arguments In a Method That Performs a Scan Operation
An argument in a method that performs a scan operation uses the following format:
%[*][width]type
The following table describes usage of the * (asterisk) and the width argument. If you specify the width, then the input is an array of characters of the length that you specify.
Argument | Description |
---|---|
* |
Suppresses assigning this value to any argument. |
width |
Sets the maximum number of characters to read. If the method encounters a white-space character or a nonconvertible character, then it stops reading these characters. For more information, see Use White Space to Improve Readability. |
The following table describes the values you can use for the type argument.
Type Value | Description |
---|---|
d,D,i,I |
Signed integer. |
u,U |
Unsigned integer. |
o,O |
Octal integer. |
x,X |
Hexadecimal integer. |
f,e,E,g,G |
Floating-point number. |
s |
String. |
[abc] |
String that includes the characters in brackets, where A–Z represents the range A to Z. |
[^abc] |
String that includes the following character in brackets: not |
Example
The following example creates a file named myfile.txt and stores a float number and a string. It then rewinds the stream and uses fscanf to read the values:
function WebApplet_Load()
{
var f;
var str;
var pFile = Clib.fopen ("c:\\myfile.txt","w+");
Clib.fprintf (pFile, "%f %s", 3.1416, "PI");
Clib.rewind (pFile);
Clib.fscanf (pFile, "%f", f);
Clib.fscanf (pFile, "%s", str);
Clib.fclose (pFile);
Clib.printf ("I have read: %f and %s \n",f,str);
}
This example produces the following output:
I have read: 3.141600 and PI