




已阅读5页,还剩57页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
系统学习 TCL 脚本入门教程 版本 版本 1 0 第 2 页 共 62 页 目目 录录 1TCL 语法语法 4 1 1简介 4 1 2运行环境 4 1 3本文约定 4 1 4参考资料 4 2引言引言 5 2 1第 1 课 简单文本输出 5 2 2第 2 课 给变量赋值 5 2 3第 3 课 命令的赋值与置换一 6 2 4第 4 课 命令的赋值与置换二 7 2 5第 5 课 命令的赋值与置换三 7 2 6第 6 课 算数运算 8 2 7第 7 课 文本比较 SWITCH 应用 9 2 8第 8 课 数值比较 IF 应用 10 2 9第 9 课 WHILE 循环 11 2 10第 10 课 FOR 循环和INCR 11 2 11第 11 课 过程 PROC 12 2 12第 12 课 过程 PROC 的参数定义 13 2 13第 13 课 变量的作用域 13 2 14第 14 课 LIST 结构 14 2 15第 15 课 LIST 项的增删改 15 2 16第 16 课 更多 LIST 相关 16 2 17第 17 课 字符串函数 17 2 18第 18 课 更多字符串函数 17 2 19第 19 课 修改字符串函数 19 2 20第 20 课 正则表达式 21 2 21第 21 课 更多正则表达式 22 2 22第 22 课 数组 24 2 23第 23 课 更多数组相关 25 2 24第 24 课 文件存取 28 2 25第 25 课 文件信息 30 2 26第 26 课 TCL 中的子进程调用 OPEN 在命令后注释用 在行开头两者均可 2 puts 输出文本 多个单词如被空格或 TAB 分隔需要使用 或 括起来 3 多个命令写在一行使用 间隔 例子 002 puts tcl ok 正确 ok 正确 ok 正确 分号和井号之间可以有空格 puts Hello 正确 puts Hello World 正确 多个单词之间不是被空格或者 TAB 分隔开 puts Hello World 这行命令运行出错 被空格分隔 puts Hello World In quotes 注释 puts Hello World In Braces 这行命令运行出错 必须使用 作为注释符号 puts This is line 1 puts this is line 2 正确 用分号分隔两个命令 puts Hello World With a semicolon inside the quotes 正确 分号在双引号内 作为字符串 一部分 2 2 第第 2 课 给变量赋值课 给变量赋值 讲解 1 set 给变量赋值 格式为 set var value 例子 003 var tcl 给变量 X 赋一个字符串 set X This is a string 给变量 Y 赋一个数字 set Y 1 24 显示 X 和 Y 的内容 puts X puts Y 打印一个分隔串 puts 第 6 页 共 62 页 打印在一行中 推荐使用双引号 set label The value in Y is puts label Y puts label Y 2 3 第第 3 课 命令的赋值与置换一课 命令的赋值与置换一 讲解 1 TCL 中命令的赋值分为置换和赋值两个步骤 2 续行符为 3 转义符同为 4 特殊字符列表 序号字符输出十六进制 1 a响铃 x07 2 b回车 x08 3 f清屏 x0c 4 n换行 x0a 5 r回车 x0d 6 t制表符 x09 7 v垂直制表符 Vertical Tab x0b 8 ddd八进制值d 0 7 9 xhh十六进制值h 0 9 A F a f 例子 004 eval tcl Show how a affects the set Z Albany set Z LABEL The Capitol of New York is puts Z LABEL Z 显示 Albany puts Z LABEL Z 显示 Z 被 转义 The next line needs a backslash to escape the puts nBen Franklin is on the 100 00 bill n 换行 100 前的 必须有 否则会将 100 作为 一个变量 提示出错 set a 100 00 puts Washington is not on the a bill This is not what you want puts Lincoln is not on the a bill 显示 100 说明是后结合的 先置换了 a 此处严格的 写应该写为 a puts Hamilton is not on the a bill 显示 a puts Ben Franklin is on the a bill 显示 100 说明是后结合的 先置换了 a 第 7 页 共 62 页 puts n examples of escape strings puts Tab tTab tTab puts This string prints out non two lines 行中 没有打印出来 如果要打印出来 需要写 成 puts This string comes out on a single line 当一行太长 不便于阅读 使用 做续行符 2 4 第第 4 课 命令的赋值与置换二课 命令的赋值与置换二 讲解 1 最外层是 则不会进行置换操作 但其中的续行符仍然有效 例子 005 escape tcl set Z Albany set Z LABEL The Capitol of New York is puts n examples of differences between and and 前的双引号前必须有 进行转义 否则这个双引号回和前面的双引号结合 导致成了 xxx and 的结构 会提示出错 puts Z LABEL Z 显示 The Capitol of New York is Albany puts Z LABEL Z 显示 Z LABEL Z 没有进行置换 中不会置换 puts n examples of differences in nesting and puts Z LABEL Z 最外层是双引号 所以进行了置换 puts Who said What this country needs is a good Z cigar 最外层是花括号 所以没有进行置换 puts n examples of escape strings puts There are no substitutions done within braces n r x0a f v puts But the escaped newline at the end of a string is still evaluated as a space 续行符仍然生效 2 5 第第 5 课 命令的赋值与置换三课 命令的赋值与置换三 讲解 1 可以传递其中的命令结果 注意不能被 包含 2 双引号包含的 中的命令可以正常执行 命令结果也可以传出 3 包含的 中的命令不会执行 更不会有命令结果传出来 例子 006 escape tcl set x abc puts A simple substitution x n 显示 abc 第 8 页 共 62 页 set y set x def 先执行 中的命令 将 def 赋值给 x 然后将该命令的结果赋值给 y puts Remember that set returns the new value of the variable X x Y y n 显示 x 和 y 都是 def set z set x This is a string within quotes within braces 由于在 中 所以并没有执行对 x 的赋值 只是将 赋值给 z puts Note the curly braces z n set a set x This is a string within braces within quotes 执行了对 x 的赋值操作 并将值传出来赋 给了 a puts See how the set is executed a puts x is x n set b set y This is a string within braces within quotes puts Note the escapes the bracket n b is b puts y is y 2 6 第第 6 课 算数运算课 算数运算 讲解 1 操作符 序号操作符解释 1 负号 正号 位操作非 逻辑非 2 乘 除 取模 3 加 减 4 循环右移 5 set Y 256 行末是否有分号都可以 set Z expr Y X 变量是否被双引号包含都可以 不过建议使用双引号 set Z expr Y X set Z LABEL Y plus X is puts Z LABEL Z puts The square root of Y is expr sqrt Y n puts Because of the precedence rules 5 3 4 is expr 3 4 5 puts Because of the parentheses 5 3 4 is expr 5 3 4 puts n more examples of differences between and puts Z LABEL expr Y X 外层是花括号不会进行置换 puts Z LABEL expr Y X 外层是双引号会进行置换 puts The command to add two numbers is expr a b 2 7 第第 7 课 文本比较 课 文本比较 SWITCH 应用应用 讲解 1 switch 的分支中的命令使用花括号包含 但是并不会影响花括号中的命令执行 切记 这是 switch 的格式 2 如果不想分支条件进行置换 需要在外加上花括号 不会影响分支中的命令执行 例子 008 switch tcl Set the variables we ll be comparing set x ONE set y 1 set z ONE This is legal switch x ONE puts ONE 1 TWO puts TWO 2 default puts NO MATCH 这种写法合法 但是阅读不便 switch x ONE puts ONE 1 TWO puts TWO 2 default puts NO MATCH 这种写法好看一些 推荐 第 10 页 共 62 页 下面这种写法 z 被置换 走入 z 的条件分支 表面上看条件分支中的命令在花括号内 这只是 switch 的一 种格式 所以其中的命令仍然被执行了 switch x z set y1 expr y 1 puts MATCH z y z is y1 ONE set y1 expr y 1 puts MATCH ONE y one is y1 TWO set y1 expr y 2 puts MATCH TWO y two is y1 THREE set y1 expr y 3 puts MATCH THREE y three is y1 default puts x does not match any of these choices This form of the command disables variable substitution in the pattern 下面为了不置换 z 在外层加上了花括号 于是走入了 ONE 分支 而分支中的命令仍然被执行了 switch x z set y1 expr y 1 puts MATCH z y z is y1 ONE set y1 expr y 1 puts MATCH ONE y one is y1 TWO set y1 expr y 2 puts MATCH TWO y two is y1 THREE set y1 expr y 3 puts MATCH THREE y three is y1 default puts x is NOT A MATCH 2 8 第第 8 课 数值比较 课 数值比较 IF 应用应用 讲解 1 条件式结果 FALSETRUE 数值0非零 yes nonoyes true falsefalsetrue 2 置换变量的方法 set y x puts y 因为是后结合并且是一次置换 所以打出来的是 x 不是 x 的值 但是在 if 的条件式中进行了二次置换 y 被置换成了 x 的值 3 注意 新行中需要写为 else 不能将 写到前一行的末尾 也不能省略 后面的那个空格 后面的 也需要写在当行 并且前面需要一个空格 例子 009 if tcl set x 1 if x 2 puts x is 2 else puts x is not 2 判断是否相等使用 if x 1 判断是否不等使用 puts x is 1 else puts x is 1 第 11 页 共 62 页 if x 1 puts GOT 1 set y x if y 1 在 if 条件式中 y 进行了二次置换 puts y is 1 在 puts 命令中 只进行了一次置换 else puts y is 1 2 9 第第 9 课 课 WHILE 循环循环 x 讲解 1 while 后面的条件表达式是放在花括号中的 放在双引号中会只执行一次置换 例子 010 while tcl set x 1 while x 5 puts x is x set x expr x 1 puts exited first loop with X equal to x n set x 0 while x 5 只执行一次置换 16 break 如果去掉这句就成了死循环 if x 3 continue 这句使 4 打不出来 puts x is x puts exited second loop with X equal to x n 2 10第第 10 课 课 FOR 循环和循环和 incr 讲解 1 incr x 和 set x expr x 1 达到一样的效果 向上加一 x 例子 011 for tcl for puts Start set i 0 i 2 incr i puts I after incr i 第一部分只执行一次 后面两部分 每次循环都会执行 puts I inside first loop i 第 12 页 共 62 页 for puts Start set i 3 i 2 incr i puts I after incr i 不会执行循环体中的命令 puts I inside second loop i puts Start set i 0 while i 2 puts I inside first loop i incr i puts I after incr i 2 11第第 11 课 过程课 过程 PROC 讲解 1 格式 proc name args body 2 调用方法中参数可以用花括号或者双引号包含 也可以不包含 3 在 puts 等命令中需要置换的话 需要使用方括号 例子 012 proc tcl proc sum arg1 arg2 set x expr arg1 arg2 return x 过程返回值 puts The sum of 2 3 is sum 2 3 n n 调用过程 puts The sum of 2 3 is sum 2 3 n n 出错 提示找不到第二个参数 置换过程中第一个参数 是 2 3 所以找不到第二个参数 puts The sum of 2 3 is sum 2 3 n n 输出 sum 2 3 因为没有方括号 根本没有进行置换 puts The sum of 2 3 is sum 2 3 n n 输出 sum 2 3 因为没有方括号 根本没有进行置换 sum 2 3 正确 sum 2 3 正确 sum 2 3 正确 proc for a b c puts The for command has been replaced by a puts puts The arguments were a n b n c n for set i 1 i 10 incr i 第 13 页 共 62 页 2 12第第 12 课 过程课 过程 PROC 的参数定义的参数定义 讲解 1 过程的参数赋缺省值 proc name arg1 arg2 value 2 过程的不确定个数的参数定义 proc name arg1 args 例子 013 proc tcl proc example first second args 参数定义 赋缺省值和不确定个数参数定义 if second puts There is only one argument and it is first return 1 else if args puts There are two arguments first and second return 2 else puts There are many arguments first and second and args return many set count1 example ONE set count2 example ONE TWO set count3 example ONE TWO THREE set count4 example ONE TWO THREE FOUR puts The example was called with count1 count2 count3 and count4 Arguments 2 13第第 13 课 变量的作用域课 变量的作用域 x 讲解 1 全局变量定义 global var1 2 局部变量 upvar x y 等同于 upvar 1 x y 作用有两个 一是将上一层的 x 的值赋给 y 二是将上 一层的 x 的地址赋给 y 于是修改 y 等于修改 x 1 代表作用范围 也可为 2 3 等 不能为 0 例子 014 varscope tcl proc SetPositive variable value 此处 variable 只是一个参数名 可以修改为其他的来代替变 量 upvar variable myvar 此处也可写为 upvar 1 variable myvar if value 0 puts name starts with a lowercase letter n else puts name starts with an uppercase letter n 说明 string wordstart 和 string wordend set word 1 12 123 1 的开始和结束位置 返回 0 和 1 puts wordstart string wordstart word 0 puts wordend string wordend word 0 位置 1 上的空格的开始和结束位置 返回 1 和 2 puts wordstart string wordstart word 1 puts wordend string wordend word 1 位置 2 上所在单词 12 的开始和结束位置 返回 2 和 4 puts wordstart string wordstart word 2 puts wordend string wordend word 2 位置 5 上所在单词 123 的开始和结束位置 返回 5 和 8 puts wordstart string wordstart word 5 puts wordend string wordend word 5 位置 6 上所在单词 123 的开始和结束位置 返回 5 和 8 puts wordstart string wordstart word 6 puts wordend string wordend word 6 第 20 页 共 62 页 2 19第第 19 课 修改字符串函数课 修改字符串函数 讲解 1 字符串函数 序号函数解释 1string tolower string1把 string1 转换为小写字母 2string toupper string1把 string1 转换为大写字母 3string trim string1 trimchars 去掉 string1 前后的 trimchars 字符 如果不指定 缺省为 空格 trimleft 和 trimright 一样的情况 4string trimleft string1 trimchars 去掉 string1 左边的 trimchars 字符 5string trimright string1 trimchars 去掉 string1 右边的 trimchars 字符 2 format 函数 格式 format formatstring arg1 arg2 argn 注意是 format 不是 string format 格式串列表 序号格式描述 1s字符串 2d十进制整数 3x十六进制数值 4o八进制数值 5f浮点数 6 左对齐 7 右对齐 不指定 或 缺省是右对齐 例子 020 stringmodify tcl set upper THIS IS A STRING IN UPPER CASE LETTERS set lower this is a string in lower case letters set trailer This string has trailing dots set leader This string has leading dots set both this string is nested in parens puts tolower converts this upper puts to this string tolower upper n puts toupper converts this lower puts to this string toupper lower n puts trimright converts this trailer puts to this string trimright trailer n 第 21 页 共 62 页 puts trimleft converts this leader puts to this string trimleft leader n puts trim converts this both puts to this string trim both n set a trim puts string trim a 返回 trim 说明缺省是去掉的空格 set labels format 20s 10s Item Cost 20s 总长度 20 字符串左对齐 set price1 format 20s 10d Cents Each Tomatoes 30 10d 总长度 10 数值右对齐 set price2 format 20s 10d Cents Each Peppers 20 set price3 format 20s 10d Cents Each Onions 10 set price4 format 20s 10 2f per Lb Steak 3 59997 10 2f 总长度 10 小数点后两位 数 值右对齐 puts n Example of format n puts labels puts price1 puts price2 puts price3 puts price4 2 20第第 20 课 正则表达式课 正则表达式 讲解 1 利用正则表达式在字符串中查找子串 格式 regexp switches exp string1 matchVar subMatch1 subMatchN 例如 regexp A Za z a z sample match sub1 sub2 switches exp 正则表达式 使用 包含 如果使用 包含 需要转义符才能执行 sample 被查找的字符串 match 满足 A Za z a z 匹配的子串传递给 match sub1 满足 A Za z 的子串传递给 sub1 正则表达式中圆括号的匹配项会将结果按顺序传 递给后面的变量 sub2 满足 a z 的子串传递给 sub2 正则表达式中圆括号的匹配项会将结果按顺序传递 给后面的变量 2 利用正则表达式在字符串中替换子串 格式 regsub switches exp string1 subSpec VarName 例如 regsub way sample lawsuit sample2 switches exp 正则表达式 此处是字符串 way string1 被替换的字符串 第 22 页 共 62 页 subSpec 替换为什么字符串 此处是 lawsuit VarName 替换后的结构赋给的变量 注意 只替换一次 3 正则表达式常用通配符列表 序号通配符描述 1 匹配一个字符串的开头 2 匹配一个字符串的结尾 3 匹配任意一个字符 4 匹配 0 到 n 个任意字符 5 匹配 1 到 n 个任意字符 6 匹配一个字符集合 例如 a z 代表匹配所有小写字母 7 匹配不包括该集合 例如 a z 代表匹配所有非小写字母 8 圆括号会将其中的正则表达式的匹配项传递给后面的变量 例子 021 regular tcl set sample Where there is a will There is a way set result regexp a z sample match puts Result result match match 返回 here set result regexp A Za z a z sample match sub1 sub2 puts Result result Match match 1 sub1 2 sub2 sub1 Where sub2 there set result regexp A Za z a z sample match sub1 sub2 sub3 puts Result result Match match 1 sub1 2 sub2 3 sub3 返回的结果为 sub1 Where sub2 是一个空格 sub3 there 说明加上圆括号就能将匹配子串传递给后面的变量 regsub way sample lawsuit sample2 puts New sample2 set sample Where there is a will There is a way way regsub way sample lawsuit sample2 puts New sample2 返回结果 There is a lawsuit way 可以看出只是替换了左边的一个 way 2 21第第 21 课 更多正则表达式课 更多正则表达式 讲解 第 23 页 共 62 页 正则表达式非常重要 虽然已经有正则表达式的工业标准 但是实际情况并不完全统一 perl shell tcl java 等的正则表达式都有些许区别 所以大家需要留心细节 1 0 到 n 个非零字符 2 0 到 n 个零 3 0 9 1 到 n 个数字 4 0 到 n 个非反斜杠字符 5 a z 前面是一个反斜杠 后面跟着 0 到 n 个小写字母 6 t 非制表符的任意字符 7 t 一个制表符 8 其实是匹配的 因为书写的时候需要转义符 例子 set list1 list dev wd0a 17086 10958 5272 68 dev wd0f 179824 127798 48428 73 news dev wd0h 1249244 967818 218962 82 usr dev wd0g 98190 32836 60444 35 var foreach line list1 regexp 0 9 a z line match size mounted puts mounted is size blocks get every field value regexp line match first regexp 0 9 line match first regexp 0 9 0 9 line match first regexp 0 9 0 9 0 9 0 9 line match first regexp 0 9 0 9 0 9 0 9 a z line match first regexp a z line match first puts first get two number regexp 0 9 a 3343a match ss puts ss set line Interrupt Vector 32 0 x20 regexp t t 0 9 0 x 0 9a fA F line match hexval 看得人头晕 建议各位还是用花 括号吧 puts Hex Default is 0 x hexval set str2 abc def regexp a f def str2 match 第 24 页 共 62 页 regexp a z a z str2 match puts using a f the match is match regexp a f def str2 match regexp a f def str2 match puts using a f the match is match regsub str2 is followed by str3 puts str2 with the substituted is str3 regsub a f a f str2 2 follows 1 str3 puts str2 is converted to str3 注意 1 和 的区别 ab 代表的是 ab 串 也就是说 ab 是有顺序的 ab 代表的是 a b 等 代表的是不确定的数个字符 没有顺序问题 2 a z 代表的是非 a z 的任意字符 a z 代表的是 a z 及 字符 此处的 不代表非的意思 它和 a b c 一样是个单独的字符 3 a z 和 a z 的区别 例子 regexp 0 9 333 match ss puts ss 有括号才有返回 没有括号就没有返回 2 22第第 22 课 数组课 数组 讲解 1 数组相关命令 序号命令描述 1array exists arrayName判断一个数组是否存在 数组存在返回 1 数组不存在返回 0 2array names arrayName pattern返回一个数组的指示列表 相当于数组的第一维 如果没有匹配 串则完全返回 3array size arrayName返回数组的列数 相当于数组的第二维数目 4array get arrayName取得数组的值列表 它使数组的赋值变得简单 例如 array set arrayX array get arrayY 实现了将 arrayY 赋值给 arrayX 5array set arrayName datalist数组定义 第 25 页 共 62 页 例子 023 array tcl array set array1 list 123 Abigail Aardvark 234 Bob Baboon 345 Cathy Coyote 456 Daniel Dog puts Array1 has array size array1 entries n puts Array1 has the following entries n array names array1 n puts ID Number 123 belongs to array1 123 n set array1 123 modified set array1 123 modified puts ID Number 123 belongs to array1 123 n array set array3 array get array1 可以看出 tcl 中的数组赋值非常简单 不用写循环来 赋值 puts Array3 has array size array3 entries n puts Array3 has the following entries n array names array3 n puts Array3 has the following entries while using pattern n array names array3 2 n 使用匹配串 puts ID Number 123 belongs to array3 123 n if array exist array1 puts array1 is an array else puts array1 is not an array if array exist array2 puts array2 is an array else puts array2 is not an array 2 23第第 23 课 更多数组相关课 更多数组相关 讲解 1 使用 foreach 浏览数组内容 2 使用 array startsearch array anymore 和 array nextelement 浏览数组内容 3 数组相关函数列表 序号函数描述 1array startsearch arrayName得到数组第一项 返回的是 id 2array nextelement arrayName searchID得到数组的下一项 返回的是 id 第 26 页 共 62 页 3array anymore arrayName searchID根据当前的 id 判断是否还有内容 返回 1 为找 到 返回 0 为没有找到 4array donesearch arrayName searchID根据 id 查找相应项 会破坏相应的状态信息 4 global 和 upvar 的使用注意事项 globalupvar 函数内用 用 普通变量 函数外用 不用 函数内用 用 数组变量 函数外不用 不用 解释 除了 global 普通变量函数内外都用 其他都是函数外不用 函数内用 特例不考虑 下面有详细的例子 例子 024 array tcl array set array1 list 123 Abigail Aardvark 234 Bob Baboon 345 Cathy Coyote 456 Daniel Dog Simply iterating through an array with a foreach loop foreach id array names array1 浏览数组内容方法一 puts array1 id has ID id Without sorted even turn is not original Two procs iterating through the same array with iteration commands proc getrec format1 arrayVar searchid global arrayVar upvar searchid id 注意这里的 id 并非在上一层次中已经定义的一个变量 他只是表明通过参数修改的是上一级堆栈的变量 此处 id 随便改成一个上一级堆栈并未定义的变量名是 没有问题的 可以试试将 id 改为 id1 set record array nextelement arrayVar id return The current ID is record proc getrec format1 arrayVar searchid 这种写法是其他语言最常用的写法 但 是 tcl 中是错误的 数组的传递与其他简单类型不同 第 27 页 共 62 页 set record array nextelement arrayVar searchid return The current ID is record proc getrec format2 arrayVar searchid global arrayVar upvar searchid id set record array nextelement arrayVar id return record set searchId array startsearch array1 123 开始 浏览数组内容方法二 puts set item 0 while array anymore array1 searchId incr item if expr item 2 set format1 getrec format1 array1 searchId puts item number item format 1 format1 else set format2 getrec format2 array1 searchId puts item number item format 2 format2 普通类型 global 传值 proc tryglobal glo global glo return The current glo is glo set glo1 12341234 puts tryglobal glo1 返回 glo1 而用 upvar 就不加 在前面 puts tryglobal glo1 返回 12341234 普通类型 upvar 传值 proc tryupvar upv upvar upv upv1 return The current upv is upv1 第 28 页 共 62 页 set upv1 1234512345 puts tryupvar upv1 返回 1234512345 puts tryupvar upv1 出错了 定义一个数组后面用 array set arr1 list 1 2 3 4 会被理解为二维的 即 1 3 是 array names 2 4 是值 数组类型 global 传值 proc tryarrglobal arrglo global arrglo return The current glo is array names arrglo puts tryarrglobal arr1 函数中 arrglo 空 puts tryarrglobal arr1 函数中 arrglo 出错了 puts tryarrupvar arr1 函数中 arrglo 正确 返回 1 3 puts tryarrupvar arr1 函数中 arrglo 出错了 数组类型 upvar 传值 proc tryarrupvar arrglo upvar arrglo arrglo1 return The current glo is array names arrglo1 puts tryarrglobal arr1 函数中 arrglo1 正确 返回 1 3 puts tryarrglobal arr1 函数中 arrglo1 arrglo 出错了 puts tryarrupvar arr1 函数中 arrglo1 正确 返回 1 3 建议 puts tryarrupvar arr1 函数中 arrglo1 出错了 参数传递非常容易混乱 所以举的例子 没有用文字说明 下面来总结一下 global 普通变量 函数内 用 函数外 用 数组 函数内 用 函数外 不用 upvar 普通变量 函数内 用 函数外 不用 数组 函数内 用 函数外 不用 或 函数内 不用 函数外 不用 一句话 除了 global 普通变量函数内外都用 其他都是函数外不用 函数内用 特例不考虑 2 24第第 24 课 文件存取课 文件存取 讲解 第 29 页 共 62 页 1 文件打开命令 格式 openfile fileName access permission 解释 fileName 文件名称 access 存取模式 序号存取模式描述 1r打开文件读 文件必须已经存在 2r 打开文件读写 文件必须已经存在 3w打开文件写 如果文件不存在创建一个 长度设为零 4w 打开文件读写 如果文件不存在创建一个 长度设为零 5a打开文件写 文件必须已经存在 位置指到文件末尾 6a 打开文件读写 如果文件不存在 创建一个 位置指到文件末尾 permission 权限 举一个例子 000 000 000 第一组三位为 user 权限 第二组三位为同组其他用户的权限 第三组三位为其他组所 有人的权限 每个三位的权限依次代表读 写 执行 如果有相应的权限就设置为一 没有设置为 0 然 后三位为组转成十进制数 2 文件关闭命令 文件用完后一定要记得关闭 格式 close fileID 3 读取文件内容 格式 gets fileID varName fileID 可以为以下四种中的一种 通过 open 打开文件得到的文件标志符 stdin 标准输入 stdout 标准输出 stderr 标准错误 varName 读出的文件内容存入 4 向文件写入内容 格式 puts nonewline fileID string fileID 可以是 通过 open 打开文件得到的文件标志符 stdout stderr 5 读入文件内容 功能 读入文件所有剩下的内容 格式 read nonewline fileID 解释 如果使用了 nonewline 读入每一行前会先去掉最后一个字符 功能 读入指定字节数的文件内容 格式 read fileID numBytes 6 改变指向文件中的位置 格式 seek fileID offset orgin 第 30 页 共 62 页 offset 偏移量 orgin 偏移量的相对位置 start 文件开头位置 current 文件当前位置 end 文件结束位置 7 其他文件函数 功能 返回文件位置 格式 tell fileID 功能 刷新缓存区的内容 将内存中缓冲区的内容写到硬盘上 格式 flush fileID 功能 判断是否在文件末尾 格式 eof fileID 例子 025 file tcl set file 025 file txt set fileid open file w 得到文件的标志符 seek fileid 0 start 定位到
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 三维激光摊铺检测技术
- 风险事故应急预案
- 策划经理年度工作总结
- 商业道德规范
- 物业工程技能培训
- 培训行业金牌顾问
- CRRT的应用和护理
- 2025春节前公司安全教育培训
- 管理教育培育培训课程
- 糖尿病的护理
- 多媒体设备日常维护与维修服务方案
- 卷烟工厂MES系统技术方案
- 辊压机培训ppt课件
- 译林小学英语5B教材分析
- 江苏省常州市2024届高一数学下学期期末质量调研试题(含解析)
- 新标准大学英语(第二版)综合教程2 Unit 1 A篇练习答案及课文翻译
- 冀教版英语小升初模拟试卷
- 食品用塑料包装容器工具等制品生产许可审查细则
- 财政部金融企业不良资产批量转让管理办法(财金[2012]6号)
- 物流供应商运作考评标准
- 招标投标活动异议和投诉处理工作规范
评论
0/150
提交评论