@weintek/libc/stdio
The @weintek/libc/stdio module provides buffered I/O for reading from and writing to files.
Influenced by the C standard library.
Note: Available when a Custom Model script runs on a local HMI.
Import
const cstdio = await import('@weintek/libc/stdio');Example: Create a file and write to it
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);Example: Open a file and read its content
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);Types
FILE
An opaque file descriptor returned by fopen(). Pass it to other stdio functions to operate on the file.
Functions
fopen(disk, filename, mode)
Opens a file on the specified disk with the given mode.
Parameters
| Name | Type | Description |
|---|---|---|
disk | String | Target disk: SD, USB1, or USB2. |
filename | String | Path of the file to open. |
mode | String | Operation mode (see table below). |
Mode values
| Mode | Operation |
|---|---|
r | Read |
w | Write |
a | Append |
r+ | Read and Write |
w+ | Write and Read |
a+ | Append and Read |
Returns: FILE — A file stream. On error, an exception is thrown.
fclose(file)
Closes the given file descriptor.
| Name | Type | Description |
|---|---|---|
file | FILE | File descriptor to close. |
fread(file, buffer, position?, length?)
Reads from the file descriptor into a buffer.
| Name | Type | Description |
|---|---|---|
file | FILE | File descriptor to read. |
buffer | ArrayBuffer | Buffer to store data in. |
position | Number | Optional. Offset in the buffer to start storing. Default: 0. |
length | Number | Optional. Bytes to read. Default: buffer_size - position. |
Returns: Number — Bytes successfully read.
fwrite(file, buffer, position?, length?)
Writes data from a buffer to the file descriptor.
| Name | Type | Description |
|---|---|---|
file | FILE | File descriptor to write. |
buffer | ArrayBuffer | Buffer containing data to write. |
position | Number | Optional. Offset in the buffer to start writing from. Default: 0. |
length | Number | Optional. Bytes to write. Default: buffer_size - position. |
Returns: Number — Bytes successfully written.
fseek(file, offset, position)
Sets the file position indicator.
| Name | Type | Description |
|---|---|---|
file | FILE | File descriptor to set. |
offset | Number | Bytes to offset from position. |
position | Number | Reference point for the offset. |
Returns: Number — 0 on success, non-zero on failure.
ftell(file)
Returns the current file position.
Returns: Number — Offset from the beginning of the file, or -1 on error.
clearerr(file)
Clears the end-of-file (EOF) and error indicators for the file descriptor.
feof(file)
Tests the EOF indicator.
Returns: Number — Non-zero if EOF indicator is set; 0 otherwise.
ferror(file)
Tests the error indicator.
Returns: Number — Non-zero if error indicator is set; 0 otherwise.
fflush(file)
Forces a write of all buffered data for the file descriptor.
Returns: Number — 0 on success; EOF on error.
fileno(file)
Returns the integer file descriptor for the given file stream.
See Linux manual page.
| Name | Type | Description |
|---|---|---|
file | FILE | The file stream to examine. |
Returns: Number — The file descriptor used to implement file.
Constants
| Constant | Description |
|---|---|
SEEK_SET | Argument to fseek() — seek from the beginning of the file. |
SEEK_CUR | Argument to fseek() — seek from the current file position. |
SEEK_END | Argument to fseek() — seek from the end of the file. |