Scan from File

Scans text in a file for data that matches a specified format, returning these matches as a specified data type.

1378

Inputs/Outputs

datatype_icon

format string

String that includes a format specifier for each output. You can also include any additional text that you know appears in the input string, but which you do not want to appear in an output.

A format specifier is a series of characters, initiated by a %, that indicates how to convert a series of characters into an output.

Each format specifier uses the following general syntax:

% widthspecifier

This input accepts a maximum of 255 characters.

Default value: Empty string — Causes this node to scan the text according to the default behavior for the data types of the wired outputs.

Specifiers

Specifier Definition
x hexadecimal integer
o octal integer
b binary integer
d signed decimal integer
u unsigned decimal integer
f floating-point number with fractional format
e floating-point number in scientific notation
g floating-point number in either fractional format (f) or scientific notation (e) depending on the exponent of the number:
  • f—The exponent is greater than -4 and less than the precision specified
  • e—exponent is less than -4 or greater than the precision specified
  • ^e or ^g floating-point number in engineering notation
    p floating-point number in SI notation
    s string. This specifier scans to the next whitespace character. If the current scanning location begins with a whitespace character, the node scans all contiguous whitespace.
    [ ]

    character combinations from within a specified set. [ ] matches a string that contains any combination of the characters specified between the brackets. Character matches are case sensitive.

    You can specify a range of characters by using a -. For example, [0-9a-zA-Z ] matches any string that contains numbers, letters, or spaces.

    You can exclude characters from the scan by using ^ as the first character in the set. For example, [^,;] matches any string of characters up to but not including the first comma or semicolon.

    To allow hyphens within the character set, specify it as the first or last character in the set.

    To allow ^ within the character set, include it anywhere other than as the first character in the set.

    T absolute time, most commonly used to interpret timestamp data
    t relative time, most commonly used to interpret numeric data as elapsed seconds

    Syntax for Matching Literal Text That You Do Not Want To Output

    Syntax Definition
    \00 - \FF Hex value of an 8-bit character; must be uppercase
    \b Backspace (ASCII BS, equivalent to \08)
    \f Form feed (ASCII FF, equivalent to \0C)
    \n Linefeed (ASCII LF, equivalent to \0A). Format into File automatically converts this code into the platform-dependent end-of-line character.
    \r Carriage return (ASCII CR, equivalent to \0D)
    \t Tab (ASCII HT, equivalent to \09)
    \s Space (equivalent to \20)
    \\ Backslash (ASCII \, equivalent to \5C)
    %% Percent sign
    non-escaped spaces Any contiguous spaces in the format string match any whitespace in the input string, including new lines and tabs.
    datatype_icon

    input file

    The file that this node uses.

    This input can be a reference to a file or an absolute file path. In both cases, this node opens the specified file without requiring you to call Open/Create/Replace File first.

    If you specify an empty path, a relative path, or a path to a non-existent file, this node returns an error.

    Default value: No value — Displays a file dialog box that prompts you to select a file.

    datatype_icon

    error in

    Error conditions that occur before this node runs.

    The node responds to this input according to standard error behavior.

    Standard Error Behavior

    Default value: No error

    datatype_icon

    default value

    Default data type and default value for the corresponding output. The node uses this information only if you do not wire a string constant to format string. If you wire a string constant to format string, the node uses the format specifiers in that constant to determine the data type of each output.

    Each default value can be a string, path, Boolean, enumerated type, timestamp, or numeric value. You cannot use arrays and clusters with this node.

    Default value: A double-precision floating point number with a value of 0 or an empty string, depending on the output data type defined by format string

    Implications of Using an Enumerated Type

    If you wire an enumerated type to default value, the node scans for substrings within the input text that match the string values in the enumerated type and returns the corresponding value of the enumerated type.

    datatype_icon

    file out

    Refnum of the file that the node read.

    datatype_icon

    error out

    Error information.

    The node produces this output according to standard error behavior.

    Standard Error Behavior

    datatype_icon

    output

    Output data obtained from a single format specifier in format string.

    The data type of this parameter matches the data type specified by the corresponding format specifier in format string or the corresponding default value. Possible data types include strings, paths, enumerated types, timestamps, and numerics, but not arrays and clusters. If you scan a string that does not fit into the numeric representation you specify, this node returns the largest number that fits into that representation.

    Examples

    input string format string default(s) output(s) remaining string

    abc, xyz

    12.3+56i 7200

    %3s, %s%f%2d

    -- abc 00
    -- xyz
    0+00i 12.3+56i
    -- 72
    Q+1.27E-3 tail Q%f t -- 1.27E-3 ail
    0123456789 %3d%3d -- 12 6789
    345
    X:9.860 Z:3.450 X:%fY:%f 100 (I32) 10 Z: 3450
    100.00 (DBL) 100.00
    set49.4.2 set%d -- 49 .4.2
    color: red color: %s blue (enum {red, green, blue}) red --
    abcd012xyz3

    %[a-z]%d

    %[a-z]%d

    -- abcd --
    12
    xyz
    3
    welcome to LabVIEW, John Smith %[^,],%s -- welcome to LabVIEW Smith
    John

    Programming Patterns

    Scanning Values with Data Types Other Than Double-Precision Floating-Point

    format string determines what data types to expect in the input text. If you do not wire a string constant to format string, you must wire the expected data type for each expected output to the corresponding default value.

    Troubleshooting "Too few/many format specifiers" Error

    These errors occur when you wire a string to format string that contains a different number of format specifiers than there are output parameters. To fix this mismatch, either resize the node to display the same number of outputs as format specifiers, or modify the number of format specifiers in format string to match the number of output parameters.

    Troubleshooting "Format specifier type mismatch" Error

    This error occurs when you wire a string to format string that contains at least one format specifier whose data type specifier is incompatible with the data type of the corresponding output. The text of the error specifies which parameter caused the mismatch. To fix this mismatch, you must change either the data type specifier in the relevant format specifier or wire a compatible data type to the relevant output.

    Removing Excluded Characters from the File Stream

    When you use the %[^...] format specifier to exclude characters from your scan, the excluded character(s) remain in the file stream. For example, %[^\n\r] matches an entire line of text, but the carriage return and new line are left behind. To remove the excluded characters from the file stream, repeat them outside of the brackets. In the previous example, %[^\n\r]\n\r removes the carriage return and new line from the file stream.

    Beginning a Scan in the Middle of a File

    Unlike Scan from String, you cannot use this node to determine a starting point for the scan. Instead, you must first use Read from Text File to access the contents of the file and then Scan from String to specify where the scan begins.