vivien luo 发表于 2013-3-31 23:28:57

新手教程-Skill中的文件操作

显示数据显示数据可通过以下两种方式1)使用print和println函数2)使用printf函数print 和 println函数Skill 编译器对每种不同数据都有默认的显示格式。print和println函数将使用默认格式显示数据。以下是显示格式样本
数据类型默认格式例子
integer(整型)5
floating point(浮点型)1.3
text string(字符型)“Mary learned SKILL”
variable(变量)bBox
list(列表)(1 2 3)
注:print 和println函数只能显示单个数据值。println 功能与print一样,但在输出结果后面会自动换行。for( i 1 3 print( “hello” )) ;Prints hello three times.输出结果为:
“hello”"hello”"hello”for( i 1 3 println( “hello” )) ;Prints hello three times输出结果为:
“hello”“hello”“hello”printf函数printf的一般格式为printf(格式控制,输出表列)printf函数通过格式控制输出结果:printf(“\n%-15s %-15s %-10d %-10d %-10d %-10d”layerName purposerectCount labelCount lineCount miscCount)第一个参数是转换控制字符串,包含各种指令:%[-][.precision]conversion_code[-] = left justify(左对齐) = minimum number of character positions(最小字符)[.precision] = number of characters to be printed conversion_code(需要打印的字符数)d – decimal(integer)f – floating points – string or symbolc – charactern – numericL – list (Ignores width and precision fields.)P – point list (Ignores width and precision fields.)B – Bounding box list (Ignores width and precision.)%L指令指定默认格式,该指令可方便的方式散布应用程序特定的默认格式。printf函数返回值为taList = ‘(1 2 3)printf( “\nThis is a list: %L” aList ) => tThis is a list: (1 2 3)aList = nilprintf( “\nThis is a list: %L” aList ) => tThis is a list: nil如果转换控制指令是不恰当的数据项,printf输出结果显示错误。printf( “%d %d” 5 nil )Message: *Error* fprintf/sprintf:format spec. incompatible with data – nil向文件中写入数据向文件中写入数据,可通过以下步骤:1.使用outfile函数,获取一个文件的输出端口2. 为print和println函数提供一个可选的端口参数写入文件,或者提供一个端口参数给fprintf函数写入文件3. 使用close函数关闭输出端口。print和println可接受一个可选的输出端口参数,使用outfile获取一个文件的输出端口。然后写入文件,最后使用close函数关闭输出端口。myPort = outfile( “myFile” ) ;向myFile文件写入内容for( i 1 3println( list( “Number:” i) myPort ))close( myPort )以上函数测试效果如下: outfile函数需要一个全路径,当未获取输出文件端口或文件未创建时,outfile函数返回nil。print和println函数返回一个错误,当其端口为nil,请看下面例子:println( “Hello” nil )Message: *Error* println: argument #2 should be an I/O port(type template = “gp”) – nil与print和println函数不一样,printf函数不需要一个可选的端口参数。使用fprintf函数向一个文件写入“格式控制”内容。fprintf函数的第一个参数必须为一个文件的输出端口。请看下面例子:myPort = outfile( “/tmp/myFile” )for( i 1 3fprintf( myPort “Number: %d\n” i ))close( myPort )Number: 1Number: 2Number: 3读取一个文件,需要以下步骤:
[*]使用infile获取一个输入端口
[*]使用gets函数一次读取文件的一行,或者使用fscanf函数转换文本输入。
[*]使用close函数关闭输入端口
使用infile函数获取一个文件的输入端口。gets函数读取文件的下一行。请看下面例子:inPort = infile( “~/.cshrc” )when( inPortwhile( gets( nextLine inPort )println( nextLine ))close( inPort ))以上函数测试结果如下: gets函数读取文件的下一行,gets函数的第一个参数变量为gets函数读取文件一行的值。gets函数返回一个字符串,当遇到文件结尾时,返回nil值。fscanf函数通过“格式控制”读取文件内容”。fscanf函数主要输入参数如下:
[*]输入端口
[*]格式控制字符串
[*]接收结果的变量
fscanf函数返回结果为相符数据项的数量。

Johnny 发表于 2013-6-28 07:18:08

skill用起来很方便,而且不容易出错,就是感觉有点难学,可能是自己经历太少了吧

冷青瓷 发表于 2013-7-16 16:36:13

很有用啊,对输出认识了很多

施主施格尔 发表于 2013-7-29 23:55:30

慢慢学习skill

duan 发表于 2013-8-12 23:56:29

楼主辛苦了!

duan 发表于 2013-8-17 21:42:36

谢谢楼主!

ecoren 发表于 2013-11-26 14:19:11

谢谢LZ         

ecoren 发表于 2013-11-26 14:21:18

受益良多         

owencai 发表于 2013-12-2 22:28:27

谢谢楼主,辛苦了,

sisic 发表于 2013-12-31 23:43:07

skill語言真的不好學阿看了很多還是不懂
页: [1] 2 3 4
查看完整版本: 新手教程-Skill中的文件操作