#136 Files in tcl

It is possible to convert a filename to the Tcl format. Notice the curly braces around the filename so that the \ character isn’t interpreted as the escape character.

set filename {C:\My_files\readme.txt};
C:\My_files\readme.txt

file join [file nativename $filename];
C:/My_files/readme.txt

While not recommended, it is also possible to escape the \ character for Windows pathnames by using another \ preceding each pathname part.

set filename "C:\\My_files\\readme.txt";
C:\My_files\readme.txt

file join [file nativename $filename];
C:/My_files/readme.txt

If the file readme.txt exists in C:/My_files, the file exists command will return a value of 1.

file exists "C:/My_files/readme.txt";
1

A file can be opened for reading.

set fileID [file open "C:/My_files/readme.txt" r];
puts "$fileID";
Tcl returns the selected fileID.

A file can be opened for reading and writing, truncating the file or creating it if it does not exist.

set fileID [file open "C:/My_files/readme.txt" w+];
puts "$fileID";
Tcl returns the selected fileID.

A file can be opened for reading and writing, appending data to the end of the file.

set fileID [file open "C:/My_files/readme.txt" a+];
puts "$fileID";
Tcl returns the selected fileID.

Discover more from Tips and Hints for Aerospace Engineers

Subscribe now to keep reading and get access to the full archive.

Continue reading