Cadence Skill 论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 39545|回复: 33

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

[复制链接]
发表于 2013-3-31 23:28:57 | 显示全部楼层 |阅读模式

显示数据

显示数据可通过以下两种方式

1)  使用printprintln函数

2)  使用printf函数

print 和 println函数

Skill 编译器对每种不同数据都有默认的显示格式。printprintln函数将使用默认格式显示数据。

以下是显示格式样本

数据类型默认格式例子
integer(整型)5
floating point(浮点型)1.3
text string(字符型)“Mary learned SKILL”
variable(变量)bBox
list(列表)(1 2 3)

注:printprintln函数只能显示单个数据值。println 功能与print一样,但在输出结果后面会自动换行。

  1. for( i 1 3 print( “hello” )) ;Prints hello three times.
复制代码
输出结果为:

“hello”"hello”"hello”

  1. 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 purpose

rectCount labelCount lineCount miscCount

)

第一个参数是转换控制字符串,包含各种指令:

%[-][width][.precision]conversion_code

[-] = left justify(左对齐)

[width] = minimum number of character positions(最小字符)

[.precision] = number of characters to be printed conversion_code(需要打印的字符数)

d – decimal(integer)

f – floating point

s – string or symbol

c – character

n – numeric

L – list (Ignores width and precision fields.)

P – point list (Ignores width and precision fields.)

B – Bounding box list (Ignores width and precision.)

%L指令指定默认格式,该指令可方便的方式散布应用程序特定的默认格式。printf函数返回值为t

aList = ‘(1 2 3)

printf( “\nthis is a list: %L” aList ) => t

This is a list: (1 2 3)

aList = nil

printf( “\nThis is a list: %L” aList ) => t

This is a list: nil

如果转换控制指令是不恰当的数据项,printf输出结果显示错误。

printf( “%d %d” 5 nil )

Message: *Error* fprintf/sprintf:

format spec. incompatible with data – nil

向文件中写入数据

向文件中写入数据,可通过以下步骤:

1.使用outfile函数,获取一个文件的输出端口

2. 为printprintln函数提供一个可选的端口参数写入文件,或者提供一个端口参数给fprintf函数写入文件

3. 使用close函数关闭输出端口。

printprintln可接受一个可选的输出端口参数,使用outfile获取一个文件的输出端口。然后写入文件,最后使用close函数关闭输出端口。

myPort = outfile( “myFile” ) ;向myFile文件写入内容

for( i 1 3

println( list( “Number:” i) myPort )

)

close( myPort )

以上函数测试效果如下:

println.png

outfile函数需要一个全路径,当未获取输出文件端口或文件未创建时,outfile函数返回nilprintprintln函数返回一个错误,当其端口为nil,请看下面例子:

println( “Hello” nil )

Message: *Error* println: argument #2 should be an I/O port

(type template = “gp”) – nil

printprintln函数不一样,printf函数不需要一个可选的端口参数。使用fprintf函数向一个文件写入“格式控制”内容。fprintf函数的第一个参数必须为一个文件的输出端口。请看下面例子:

myPort = outfile( “/tmp/myFile” )

for( i 1 3

fprintf( myPort “Number: %d\n” i )

)

close( myPort )

Number: 1

Number: 2

Number: 3

读取一个文件,需要以下步骤:

  • 使用infile获取一个输入端口
  • 使用gets函数一次读取文件的一行,或者使用fscanf函数转换文本输入。
  • 使用close函数关闭输入端口

使用infile函数获取一个文件的输入端口。gets函数读取文件的下一行。请看下面例子:

inPort = infile( “~/.cshrc” )

when( inPort

while( gets( nextLine inPort )

println( nextLine )

)

close( inPort )

)

以上函数测试结果如下:

gets.png

gets函数读取文件的下一行,gets函数的第一个参数变量为gets函数读取文件一行的值。gets函数返回一个字符串,当遇到文件结尾时,返回nil值。

fscanf函数通过“格式控制”读取文件内容”。fscanf函数主要输入参数如下:

  • 输入端口
  • 格式控制字符串
  • 接收结果的变量

fscanf函数返回结果为相符数据项的数量。


发表于 2013-6-28 07:18:08 | 显示全部楼层
skill用起来很方便,而且不容易出错,就是感觉有点难学,可能是自己经历太少了吧
发表于 2013-7-16 16:36:13 | 显示全部楼层
很有用啊,对输出认识了很多
发表于 2013-7-29 23:55:30 | 显示全部楼层
慢慢学习skill
发表于 2013-8-12 23:56:29 | 显示全部楼层
楼主辛苦了!
发表于 2013-8-17 21:42:36 | 显示全部楼层
谢谢楼主!
发表于 2013-11-26 14:19:11 | 显示全部楼层
谢谢LZ         
发表于 2013-11-26 14:21:18 | 显示全部楼层
受益良多         
发表于 2013-12-2 22:28:27 | 显示全部楼层
谢谢楼主,辛苦了,
发表于 2013-12-31 23:43:07 | 显示全部楼层
skill語言  真的不好學阿  看了很多  還是不懂
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|网站地图|Cadence Skill 论坛 ( 蜀ICP备13024417号 )

GMT+8, 2024-4-24 09:45 , Processed in 0.258238 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表