@weintek/libc/stdio

The @weintek/libc/stdio module is primarily utilized for reading and writing external files.

Note:

  • The modules under @weintek/libc (e.g., errno, stdio, string, unistd) are heavily influenced by the design of C standard library.
  • The module is availabe when JS object/action is run on Local HMI)
Examples

Create a file and write "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);

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);

Members

(static, constant) SEEK_CUR :Number

Argument to fseek indicating seeking from the current file position

Type:
  • Number

(static, constant) SEEK_END :Number

Argument to fseek indicating seeking from end of the file

Type:
  • Number

(static, constant) SEEK_SET :Number

Argument to fseek indicating seeking from beginning of the file

Type:
  • Number

Methods

(static) clearerr(file)

Since:
  • EasyBuilder Pro V6.09.01
See:

This method resets the error flags and the EOF indicator for the given file stream file.

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

The file stream to reset the error flags for.

(static) fclose(file)

Since:
  • EasyBuilder Pro V6.09.01
See:

This method closes the given file stream.

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

The file stream to close.

(static) feof(file) → {Number}

Since:
  • EasyBuilder Pro V6.09.01
See:

This method checks if the end of the given file stream has been reached.

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

The file stream to check.

Returns:

Nonzero value if the end of the stream has been reached, otherwise ​0​.

Type
Number

(static) ferror(file) → {Number}

Since:
  • EasyBuilder Pro V6.09.01
See:

This method checks the given stream for errors.

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

The file stream to check.

Returns:

Nonzero value if the file stream has errors occurred, ​0​ otherwise.

Type
Number

(static) fflush(file) → {Number}

Since:
  • EasyBuilder Pro V6.09.01
See:

This method writes any unwritten data from the stream's buffer to the associated output device.

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

The file stream to write out.

Returns:

Returns zero on success. Otherwise a negative value is returned and the error indicator of the file stream is set.

Type
Number

(static) fileno(file) → {Number}

Since:
  • EasyBuilder Pro V6.09.01
See:

This method examines the argument file and returns the integer file descriptor used to implement this file stream.

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

The file stream to examine.

Returns:

Returns the integer file descriptor associated with stream. Otherwise, returns -1 and getErrno can be used to identify the error.

Type
Number

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

Since:
  • EasyBuilder Pro V6.09.01
See:

This method opens a file indicated by disk and filename, and returns a file stream associated with that file. mode is used to determine the file access mode.

Note:

  • The number of files that can be opened simultaneously is limited to 100.
Parameters:
Name Type Description
disk String

A string specifies the target disk. Acceptable values are SD, USB1 and USB2.

filename String

A string specifies the file path.

mode String

A string specifies the file access mode.


File access flags

File access mode string Meaning Explanation Action if file already exists Action if file does not exist
"r" read Open a file for reading read from start failure to open
"w" write Create a file for writing destroy contents create new
"a" append Append to a file write to end create new
"r+" read extended Open a file for read/write read from start error
"w+" write extended Create a file for read/write destroy contents create new
"a+" append extended Open a file for read/write write to end create new
Returns:

If successful, returns the new file stream. On error, an exception is thrown.

Type
module:@weintek/libc/stdio~FILE

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

Since:
  • EasyBuilder Pro V6.09.01
See:

This method reads data into buffer from the given input stream file. The file position indicator for the stream is advanced by the number of characters read.

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

The file stream to read.

buffer ArrayBuffer

The buffer for storing data read from the file stream.

position Number <optional>
0

The position (in bytes) where to start storing the read data from.

length Number <optional>
buffer_size - position

The length (in bytes) of data to be read.

Returns:

The length (in bytes) of data read successfully, which may be less than length if an error or end-of-file condition occurs.

Type
Number

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

Since:
  • EasyBuilder Pro V6.09.01
See:

This method sets the file position indicator for the file stream file to the value pointed to by offset.

Example

Set file position indicator to beginning of the file

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

The file stream to modify.

offset Number

Number of bytes to offset from origin.

origin Number

Position used as reference for the offset. It is specified by one of the SEEK_CUR, SEEK_END, SEEK_SET.

Returns:

If successful, returns zero. Otherwise, returns a non-zero value and getErrno() can be used to identify the error.

Type
Number

(static) ftell(file) → {Number}

Since:
  • EasyBuilder Pro V6.09.01
See:

This method returns the file position indicator for the file stream file.

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

The file stream to examine.

Returns:

If successful, returns file position indicator. Otherwise, returns -1 and getErrno() can be used to identify the error.

Type
Number

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

Since:
  • EasyBuilder Pro V6.09.01
See:

This method writes data from buffer to the output stream file. The file position indicator for the stream is advanced by the number of characters written.

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

The file stream to write.

buffer ArrayBuffer

The buffer containing the data to be written.

position Number <optional>
0

The position (in bytes) where to start extracting written data from.

length Number <optional>
-1

The length (in bytes) of data to be written.

Returns:

The length (in bytes) of data written successfully, which may be less than length if an error occurs.

Type
Number

Type Definitions

FILE

Each FILE object denotes an input/output stream. Each stream is associated with a file.

Instances of the FILE object are created by the fopen method.

All FILE objects are EventEmitters (v5.2.8).

Type:
  • Object