首页/文章/ 详情

HyperWork 2024 -二次开发-Tcl语言-文件处理(11/12)

3月前浏览2196

Since Tcl/Tk scripts run on a variety of platforms, and there are so many use cases for accessing files, Tcl provides many commands for file manipulation.

The following table contains some commonly used Tcl file commands and a summary of their usage. For more in-depth explanations and a complete listing, referred to http://www.tcl.tk/man/ or to a Tcl/Tk handbook.

  • file delete 

  • Deletes the file named .

  • file dirname 

  • Returns the parent directory of the file named .

  • file exists 

  • Check for the existence of the file named name. Returns 1 if the file exists, 0 otherwise.

  • file join 

  • Joins , , etc. into a new pathname.

  • file nativename 

  • Returns the platform specific version of the file named .

  • file split 

  • Splits the file named name into its individual pathname parts.

  • open 

  • Opens a file named name for reading/writing, depending on the  arguments. Returns the channel ID.

  • close 

  • Closes .

  • gets 

  • Reads a line from  and assigns the data to variable .

  • puts 

  • Writes out  to . If  is not specified, the default output channel is used.

  • read 

  • Reads all data from .

One item to note is that Unix uses a forward slash / as a directory separator and Windows uses a backward slash \ as a directory separator. The backward slash in Tcl is a special escape character. So on both Windows and Unix, Tcl uses / as the directory separator.

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.

To support customers that use Japanese file names, the encoding command enables multi-bit characters.

set filename [encoding convertto euc-jp $filename];


来源:TodayCAEer
ACT二次开发ADSUM
著作权归作者所有,欢迎分享,未经许可,不得转载
首次发布时间:2024-08-25
最近编辑:3月前
TodayCAEer
本科 签名征集中
获赞 18粉丝 28文章 249课程 0
点赞
收藏
作者推荐

HyperMesh二次开发-一键划分适用于疲劳分析的四面体

HyperMesh二次开发-靓仔你画的四面体零件和打的螺栓都能一键完成的在这篇文章发出后,有小伙伴就提出了,在HyperMesh能否实现一键划分用于疲劳分析的四面体零件。即对于圆角部分的曲面,按照曲率划分。其余面则按照常规方式生成,并且为了提高计算效率,平均尺寸也需要提高。测试下来,在模型均导入的情况下hm和simlab两者的执行效率,网格质量,相差无几,高清大图。摸鱼热线,请后台留言:专业解决HyperWork、ANSA、META、ADAMS、STAR-CCM+软件的结构、多体、流体前后处理二次开发需求;ADAMS的路面加密;OPTIMUS优化模板开发;SDM(仿真数据管理)系统搭建;TDM(试验数据管理)系统搭建。就像前面说的,与人沟通的问题、优化的问题无法解决外,其他都能解决,就算做不到完美解决,至少能让老表少点几下鼠标。来源:TodayCAEer

未登录
还没有评论
课程
培训
服务
行家
VIP会员 学习 福利任务 兑换礼品
下载APP
联系我们
帮助与反馈