Create and write text files by converting data to strings and then writing or formatting the output. Scan existing text files to extract data of other types.

To create a text file, do the following:

  1. Convert the data to string data.
  2. If needed for your application, format the string data.
  3. Write the text data to the file.

If needed for your application, you can also create a text file by scanning an existing file and processing the data.

Tip The images on this page are VI snippets. You can drag VI snippets directly to the block diagram in LabVIEW to add that code to the block diagram. To learn how to build block diagrams yourself, refer to Block Diagram Objects.
  • To write data to a text file, convert the data to a string.

    Use the functions on the String palette to convert your data to a string. For example, the VI in Figure 30. Converting Numeric Data to String does the following:

    • Converts an array of randomly generated numbers to strings
    • Writes the strings to a text file
    Figure 30. Converting Numeric Data to String
    LabVIEW block diagram that converts a numeric array to strings and write the string data to a text file in a loop.
  • To add text to a file you create, use the Write to Text File function.

    If you wire a file path to Write to Text File, rather than a refnum, Write to Text File automatically opens and closes the file.

    Write to Text File does not format text. Most word processing applications that read text do not require formatted text. As a result, writing text to text files does not expressly require formatting.

    Figure 31. Writing Unformatted Text to a File
    LabVIEW block diagram that writes unformatted string data to a text file.
  • If your application requires text files with specific formatting, use the available functions to format string data.

    For example, you can use the Format Into File function to accomplish two actions:

    1. Format multiple data types as text
    2. Write the formatted text to a file

    The following VI acquires multiple data types and writes the data to a file.

    Figure 32. Formatting and Writing Data to a Text File
    LabVIEW block diagram that uses Format Into File to write string, numeric, and Boolean data to a text file.

    In many cases, Format Into File can replace two actions: formatting the string with Format Into String function and then writing the resulting string with Write to Text File.

  • To scan text in a file for string, numeric, path, and Boolean values and then convert the text into a data type, use the Scan From File function.

    In many cases, Scan From File can replace two actions: reading data from a file with the Read from Binary File function or Read from Text File function and then scanning the resulting string with the Scan From String function.

Functions that format text include the format string input. Use format strings and format specifiers to define how to convert input arguments.

For more information on the functions that format strings and on format string, see the following resources:

  • Formatting Strings
  • Format Specifier Syntax
  • Using Format Specifiers in Numeric Strings