Admin Production ni-theme
Current Publication

FindPattern

LabWindows/CVI

FindPattern

int FindPattern (char buffer[], int startingIndex, ssize_t numberOfBytes, char pattern[], int caseSensitive, int startFromRight);

Purpose

Searches a character buffer for a pattern of bytes. The string pattern specifies the pattern of bytes. FindPattern returns the position within the buffer where the pattern was found.

Example Code

The following example returns 4, which is the index of the second of the three occurrences of ab in the string 1ab2ab3ab4. FindPattern skips the first occurrence because startingIndex is 3. Of the two remaining occurrences, FindPattern finds the leftmost occurrence because startFromRight is zero.

ndx = FindPattern ("1ab2ab3ab4", 3, -1, "AB", 0, 0);

The following example returns 7, which is the index of the rightmost occurrence of ab, because startFromRight is a nonzero value.

ndx = FindPattern ("1ab2ab3ab4", 3, -1, "AB", 0, 1);

Parameters

Input
Name Type Description
buffer char [] Buffer to search for the pattern.
startingIndex int Zero–based index specifying the location within buffer at which to begin searching for the pattern.
numberOfBytes ssize_t Number of bytes to search.

If numberOfBytes is –1, FindPattern searches the set of bytes that starts at position startingIndex of buffer up to the first ASCII NUL.

The function returns an error if you pass a value greater than UINT_MAX.
pattern char [] NUL–terminated string specifying the pattern of bytes to search for.
caseSensitive int Specify a nonzero value or select Yes in the function panel to match characters on a case-sensitive basis.

Specify 0 or select No in the function panel to match characters on a case-insensitive basis.
startFromRight int Specify a nonzero value or select Yes in the function panel to find the rightmost occurrence of the pattern in the buffer.

Specify 0 or select No in the function panel to find the leftmost occurrence of the pattern in the buffer.

Return Value

Name Type Description
result int Index in buffer where FindPattern finds the first byte of the pattern. The index is zero–based and is relative to the first byte of the buffer variable.

Note  If FindPattern does not find the pattern in the buffer, or an error occurs, the function returns –1.

Additional Information

Library: Formatting and I/O Library

Include file: formatio.h

LabWindows/CVI compatibility: LabWindows/CVI 3.0 and later

Examples

Refer to the following examples that use the FindPattern function:

  • apps\daqmthread\daqMT.cws

    Open example
  • toolbox\msgdemo.cws

    Open example