wangjicheng886的个人空间 https://blog.eetop.cn/1774723 [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

skill language user guide 42--44

已有 281 次阅读| 2023-6-17 20:40 |系统分类:芯片设计

Writing Data to a File

To write text data to a file

1. Use the outfile function to obtain an output port on a file. 

2. Use an optional output port parameter to the print and println functions 

    and/or use a required port parameter to the fprintf function. 

3. Close the output port with the close function.


abtain [əbˈteɪn] v 获得; (尤指经努力)赢得; 存在;

an output port  输出端口


Both print and println accept an optional second argument, which should be an output 

port associated with the target file. Use the outfile function to obtain an output port for a 

file. Once you are finished writing data to the file, use the close function to release the port. 

print”和“println”函数都接受一个可选的第二个参数,该参数应为与目标文件关联的输出端口。使用“outfile”函数获取文件的输出端口。在完成向文件写入数据后,请使用“close”函数释放该端口。


The following code:


myPort = outfile( "/tmp/myFile" )

for( i 1 3 

    println( list( "Number:" i) myPort ) 

   ) ;for

close( myPort )


outfile()   close()  for()

这段代码的作用是先打开一个名为"/tmp/myFile"的文件,然后使用一个循环来向该文件中写入三行数据,

每行数据由"Number:"和当前迭代次数 i 组成。最后,通过调用close(myPort)函数关闭文件。

注意:在执行该代码之前,确保在程序运行环境中存在名为 "tmp" 的目录,并且具有对该目录的写入权限。


writes this data to the file /tmp/myFile

("Number:" 1)  

("Number:" 2)  

("Number:" 3)


Notice how SKILL displays a port: 

myPort = outfile( "/tmp/myFile" ) 

port:"/tmp/myFile"


Use a full path with the outfile function. Keep in mind that outfile returns nil if you 

don’t have write access to the file or if it can’t be created in the directory specified in the path. 

使用 outfile 函数时,请使用完整路径。请记住,如果您没有对文件的写访问权限,

或者无法在指定路径的目录中创建文件,则 outfile 函数将返回 nil。

The print and println functions display an error if the port argument is nil. 

print 和 println 函数在 port 参数为 nil 时会显示错误。

Notice that the type template uses a p character to indicate a port is expected.

请注意,类型模板使用 p 字符表示需要一个端口。

println( "Hello" nil )  

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

       (type template = "gp") - nil


Unlike the print and println functions, the printf function does not accept an optional port argument. 

与 print 和 println 函数不同,printf 函数不接受可选的端口参数。

Use the fprintf function to write formatted data to a file.

使用 fprintf 函数向文件中写入格式化的数据。

 Its first argument should be an output port associated with the file. 

它的第一个参数应该是与文件相关联的输出端口。

The following code:

myPort = outfile( "/tmp/myFile" )  

    for( i 1 3  

      fprintf( myPort "Number: %d\n" i )  

      )  ;for

  close( myPort )


1)创建名为myPort的输出端口,并将其与路径为"/tmp/myFile"的文件相关联。

2)使用循环,从1到3的范围内,依次遍历变量i。

3)在每次循环中,使用fprintf函数将格式化的字符串写入myPort所关联的文件。字符串中的"%d"表示一个整数占位符,它会被i的值替          代。换行符"\n"用于在每个数值后插入一个新行。

4)当循环结束后,关闭与myPort关联的文件。

5)注意和上面的"println"打印输出的格式区别


writes this data to the file /tmp/myFile.

Number: 1  

Number: 2  

Number: 3














点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 28

    粉丝
  • 7

    好友
  • 19

    获赞
  • 8

    评论
  • 130

    访问数

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-6-17 16:33 , Processed in 0.038540 second(s), 7 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部