AWTK fscript 中的文件流扩展函数

fscript 是 AWTK 内置的脚本引擎,开发者可以在 UI XML 文件中直接嵌入 fscript 脚本,提高开发效率。本文介绍一下 fscript 中的 文件流扩展函数。

1.istream_file_create

创建文件输入流对象。


原型
js 复制代码
istream_file_create(filename, mode) => object
示例
js 复制代码
var a = istream_file_create("test.bin", "rb");

2.ostream_file_create

创建文件输出流对象。


原型
js 复制代码
ostream_file_create(filename, mode) => object
示例
js 复制代码
var a = ostream_file_create("test.bin", "wb+");

完整示例

js 复制代码
var a = ostream_file_create("test.bin", "wb+")
assert(ostream_write_uint8(a, 1) == 1)
assert(ostream_write_int8(a, -1) == 1)
assert(ostream_tell(a), 2)

assert(ostream_write_uint16(a, 2) == 2)
assert(ostream_write_int16(a, -2) == 2)
assert(ostream_tell(a), 6)

assert(ostream_write_uint32(a, 3) == 4)
assert(ostream_write_int32(a, -3) == 4)
assert(ostream_tell(a), 14)

assert(ostream_write_uint64(a, 4) == 8)
assert(ostream_write_int64(a, -4) == 8)
assert(ostream_tell(a), 30)

assert(ostream_write_float(a, 5) == 4)
assert(ostream_write_double(a, -5) == 8)
assert(ostream_tell(a), 42)

assert(ostream_write_string(a, "hello") == 5)
assert(ostream_write_binary(a, "world", 5) == 5)
assert(ostream_tell(a), 52)

assert(ostream_flush(a))

a = istream_file_create("test.bin", "rb")
assert(istream_read_uint8(a)==1)
assert(istream_read_int8(a)==-1)
assert(istream_tell(a), 2)

assert(istream_read_uint16(a)==2)
assert(istream_read_int16(a)==-2)
assert(istream_tell(a), 6)

assert(istream_read_uint32(a)==3)
assert(istream_read_int32(a)==-3)
assert(istream_tell(a), 14)

assert(istream_read_uint64(a)==4)
assert(istream_read_int64(a)==-4)
assert(istream_tell(a), 30)

assert(istream_read_float(a)==5)
assert(istream_read_double(a)==-5)
assert(istream_tell(a), 42)

assert(istream_read_string(a, 5)=="hello")
assert(istream_tell(a), 47)

istream_read_binary(a, 5)
assert(istream_tell(a), 52)
assert(istream_seek(a, 0))
assert(istream_read_uint8(a)==1)
assert(istream_read_int8(a)==-1)
assert(istream_tell(a) == 2)

unset(a)
相关推荐
李先静17 小时前
经典 PLC 程序(3) - 延时启动和停止
st·awtk·awplc
另一种开始7 个月前
AWTK 嵌入式Linux平台实现多点触控缩放旋转以及触点丢点问题解决
awtk
另一种开始9 个月前
AWTK-MVVM 如何让多个View复用一个Model记录+关于app_conf的踩坑
awtk
李先静1 年前
如何用 SSH 访问 QNX 虚拟机
ssh·awtk·qnx
李先静1 年前
用 gdbserver 调试 arm-linux 上的 AWTK 应用程序
linux·arm开发·awtk
李先静1 年前
在树莓派 Pico 上运行 AWTK
pico·awtk
李先静1 年前
AWTK-WIDGET-WEB-VIEW 实现笔记 (3) - MacOS
macos·webview·awtk
李先静1 年前
AWTK-WIDGET-WEB-VIEW 实现笔记 (2) - Windows
webview·awtk
李先静1 年前
AWTK fscript 中的 JSON 扩展函数
json·awtk·fscript