@weintek/libc/stdio

@weintek/libc/stdio 模块主要用于读取和写入外部文件。

注意:

  • @weintek/libc 目录下的模块(例如 errno、stdio、string、unistd) 受到 C 标准库 设计的强烈影响。
  • 当 JS 元件/动作在本地 HMI 上运行时,该模块可用。
Examples

创建一个文件并写入 "Hello, World!"

const cstdio = await import('@weintek/libc/stdio');
const encoder = new TextEncoder();
const file = cstdio.fopen('USB1', '/test/hello.txt', 'w');
cstdio.fwrite(file, encoder.encode('Hello, World!').buffer);
cstdio.fclose(file);

打开一个文件并读取其内容

const cstdio = await import('@weintek/libc/stdio');
const decoder = new TextDecoder();
const buffer = new ArrayBuffer(1024);
const file = cstdio.fopen('USB1', '/test/hello.txt', 'r');
let length = cstdio.fread(file, buffer);
if (length > 0) {
    console.log(decoder.decode(new Int8Array(buffer, 0, length)));
}
cstdio.fclose(file);

Members

(static, constant) SEEK_CUR :Number

传递给 fseek 的参数,表示从当前文件位置开始查找

Type:
  • Number

(static, constant) SEEK_END :Number

传递给 fseek 的参数,表示从文件末尾开始查找

Type:
  • Number

(static, constant) SEEK_SET :Number

传递给 fseek 的参数,表示从文件开头开始查找

Type:
  • Number

Methods

(static) clearerr(file)

Since:
  • EasyBuilder Pro V6.09.01
See:

此方法重置给定文件流 file 的错误标志和 EOF 指示器。

Parameters:
Name Type Description
file module:@weintek/libc/stdio~FILE

要重置错误标志的文件流。

(static) fclose(file)

Since:
  • EasyBuilder Pro V6.09.01
See:

该方法关闭指定的文件流。

Parameters:
Name Type Description
file module:@weintek/libc/stdio~FILE

要关闭的文件流。

(static) feof(file) → {Number}

Since:
  • EasyBuilder Pro V6.09.01
See:

此方法检查给定文件流是否已到达文件末尾。

Parameters:
Name Type Description
file module:@weintek/libc/stdio~FILE

要检查的文件流。

Returns:

如果到达流的末尾,则返回非零值,否则返回 0。

Type
Number

(static) ferror(file) → {Number}

Since:
  • EasyBuilder Pro V6.09.01
See:

此方法检查给定流是否发生错误。

Parameters:
Name Type Description
file module:@weintek/libc/stdio~FILE

要检查的文件流。

Returns:

如果文件流发生错误,则返回非零值,否则返回 0。

Type
Number

(static) fflush(file) → {Number}

Since:
  • EasyBuilder Pro V6.09.01
See:

此方法将流缓冲区中的任何未写入数据写入关联的输出设备。

Parameters:
Name Type Description
file module:@weintek/libc/stdio~FILE

要写出的文件流。

Returns:

成功时返回 0,否则返回负值,并设置文件流的错误指示器。

Type
Number

(static) fileno(file) → {Number}

Since:
  • EasyBuilder Pro V6.09.01
See:

此方法检查参数 file,并返回用于实现此文件流的整数文件描述符。

Parameters:
Name Type Description
file module:@weintek/libc/stdio~FILE

要检查的文件流。

Returns:

成功时返回与流关联的整数文件描述符,否则返回 -1, 并且可以使用 getErrno 识别错误。

Type
Number

(static) fopen(disk, filename, mode) → {module:@weintek/libc/stdio~FILE}

Since:
  • EasyBuilder Pro V6.09.01
See:

该方法打开由 disk 和 filename 指定的文件,并返回与该文件关联的文件流。 mode 用于确定文件访问模式。

注意:

  • 可以同时打开的文件数量限制为 100。
Parameters:
Name Type Description
disk String

指定目标磁盘的字符串。可接受的值为 SD、USB1 和 USB2。

filename String

指定文件路径的字符串。

mode String

指定文件访问模式的字符串。


文件访问模式

文件访问模式 含义 说明 文件已存在时的操作 文件不存在时的操作
"r" 读取 以读取模式打开文件 从头开始读取 无法打开
"w" 写入 创建文件以进行写入 清空内容 创建新文件
"a" 追加 追加到文件 追加到末尾 创建新文件
"r+" 读取扩展 以读/写模式打开文件 从头开始读取 出错
"w+" 写入扩展 以读/写模式创建文件 清空内容 创建新文件
"a+" 追加扩展 以读/写模式打开文件 追加到末尾 创建新文件
Returns:

成功时返回新的文件流,错误时抛出异常。

Type
module:@weintek/libc/stdio~FILE

(static) fread(file, buffer, positionopt, lengthopt) → {Number}

Since:
  • EasyBuilder Pro V6.09.01
See:

此方法从给定的输入流 file 读取数据到 buffer。 该流的文件位置指示器将根据读取的字符数进行推进。

Parameters:
Name Type Attributes Default Description
file module:@weintek/libc/stdio~FILE

要读取的文件流。

buffer ArrayBuffer

用于存储从文件流中读取的数据的缓冲区。

position Number <optional>
0

存储读取数据的起始位置(以字节为单位)。

length Number <optional>
buffer_size - position

需要读取的数据长度(以字节为单位)。

Returns:

成功读取的数据长度(以字节为单位)。如果发生错误或遇到文件结尾, 可能会少于 length。

Type
Number

(static) fseek(file, offset, origin) → {Number}

Since:
  • EasyBuilder Pro V6.09.01
See:

此方法将文件流 file 的文件位置指示器设置为 offset 指向的值。

Example

将文件位置指示器设置到文件开头

cstdio.fseek(file, 0, cstdio.SEEK_SET);
Parameters:
Name Type Description
file module:@weintek/libc/stdio~FILE

要修改的文件流。

offset Number

从 origin 处偏移的字节数。

origin Number

作为 offset 参考位置。其值由以下之一指定: SEEK_CUR, SEEK_END, SEEK_SET。

Returns:

成功时返回 0,否则返回非零值,并且可以使用 getErrno() 识别错误。

Type
Number

(static) ftell(file) → {Number}

Since:
  • EasyBuilder Pro V6.09.01
See:

此方法返回文件流 file 的文件位置指示器。

Parameters:
Name Type Description
file module:@weintek/libc/stdio~FILE

要检查的文件流。

Returns:

成功时返回文件位置指示器,否则返回 -1,并且可以使用 getErrno() 识别错误。

Type
Number

(static) fwrite(file, buffer, positionopt, lengthopt) → {Number}

Since:
  • EasyBuilder Pro V6.09.01
See:

此方法将 buffer 中的数据写入输出流 file。该流的文件位置指示器 将根据写入的字符数进行推进。

Parameters:
Name Type Attributes Default Description
file module:@weintek/libc/stdio~FILE

要写入的文件流。

buffer ArrayBuffer

包含要写入数据的缓冲区。

position Number <optional>
0

提取要写入数据的起始位置(以字节为单位)。

length Number <optional>
-1

需要写入的数据长度(以字节为单位)。

Returns:

成功写入的数据长度(以字节为单位)。如果发生错误, 可能会少于 length。

Type
Number

Type Definitions

FILE

每个 FILE 对象表示一个输入/输出流,每个流都与一个文件关联。

FILE 对象的实例由 fopen 方法创建。

所有 FILE 对象都是 EventEmitter (v5.2.8)。

Type:
  • Object