Opening, Processing, and Closing Files

What to Use

What to Do

Create the following diagram to programmatically open, write to, and close a text file.

Customize the gray sections for your unique programming goals.


1378

1378 Open, create, or replace a file by using the Open/Create/Replace File node and specifying its precise behavior. To access the enumerated list of possible behaviors, create a constant from the operation input of the node.
1378 After the file opens, a unique identifier called a refnum represents the file. A file refnum is required for most nodes that process files.
1378 Process the file by using a file I/O node from the Storage palette category. You can interact with binary or text files. In this example, Write to Text File writes Hello world to the file represented by the file refnum.
1378 Close the file to release the object from memory and avoid potential errors. If you do not close a file, the reference cannot be closed until the VI that opened the file finishes executing.

Troubleshooting

  • Some file I/O nodes are considered high-level nodes and perform all three steps of a file I/O process—open, read/write, and close. Avoid placing high-level file I/O nodes, such as Read from Spreadsheet File and Write to Spreadsheet File, in loops because these nodes perform open and close operations each time they run.