Intel® Quartus® Prime Standard Edition User Guide: Scripting

Download
ID 683325
Date 9/24/2018
Public
Document Table of Contents

2.13.9. File I/O

Tcl includes commands to read from and write to files. You must open a file before you can read from or write to it, and close it when the read and write operations are done.

To open a file, use the open command; to close a file, use the close command. When you open a file, specify its name and the mode in which to open it. If you do not specify a mode, Tcl defaults to read mode. To write to a file, specify w for write mode.

Open a File for Writing

set output [open myfile.txt w]

Tcl supports other modes, including appending to existing files and reading from and writing to the same file.

The open command returns a file handle to use for read or write access. You can use the puts command to write to a file by specifying a file handle.

Write to a File

set output [open myfile.txt w]
puts $output "This text is written to the file."
close $output

You can read a file one line at a time with the gets command. The following example uses the gets command to read each line of the file and then prints it out with its line number.

Read from a File

set input [open myfile.txt]
set line_num 1
while { [gets $input line] >= 0 } {
	# Process the line of text here
	puts "$line_num: $line"
	incr line_num
}
close $input

Did you find the information on this page useful?

Characters remaining:

Feedback Message