Melang

Logo

A script language of time-sharing scheduling coroutine in single thread

View the Project on GitHub Water-Melon/Melang

File

import

f = Import('file');

File is implemented as a Set named File.

File {
  fd;
  errno;
  open(path, op, prio);
  lseek(offset, whence);
  read(nbytes);
  write(data);
  close();
  errmsg();
  size();
};
fd

fd is the file descriptor that is like an integer in C. But in Melang, this property is read-only. It will be set by method open. Usually, we don’t have to access it.

errno

errno is an integer as in C. This number is an error number set by every file operation method in this Set.

open

Open a file in the specified mode.

Input:

Return value:

lseek

Repositions the offset of the file descriptor to the argument offset, according to the directive whence.

Input:

Return value:

read

Read nbytes from current position of the openned file.

Input:

Return value:

write

Write data in the file.

Input:

Return value:

close

Close file.

Input: None

Return value:

errmsg

Get error message.

Input: None

Return value:

size

Get file size.

Input: None

Return value:

Example

Create a test file before Melang execution.

$ echo "hello" > tempfile

Execute Melang program:

sys = Import('sys');
F = Import('file');
f = $F; // or f = $File; both are the same.
if (f.open('tempfile', 'rw+') != false) { //read and write file and file content won't be ereased
  f.lseek(1, 'begin');
  f.write('hi all');
  f.lseek(0, 'begin');
  sys.print(f.read(f.size()));
} fi

The output is:

hi all

And the content in tempfile is:

hi all