LuaSTG-x Core API
io Class Reference

Public Member Functions

static function close (local file)
 
static function flush ()
 
static function input (local file)
 
static function lines (local filename, local ...)
 
static function open (local filename, local mode)
 
static function output (local file)
 
static function popen (local prog, local mode)
 
static function read (local ...)
 
static function tmpfile ()
 
static function type (local obj)
 
static function write (local ...)
 

Public Attributes

file stderr
 
file stdin
 
file stdout
 

Detailed Description

I/O 库提供了两套不同风格的文件处理接口。

第一种风格使用隐式的文件句柄;

它提供设置默认输入文件及默认输出文件的操作,

所有的输入输出操作都针对这些默认文件。

第二种风格使用显式的文件句柄。

当使用隐式文件句柄时,所有的操作都由表 io 提供。

若使用显式文件句柄, io.open 会返回一个文件句柄,

且所有的操作都由该文件句柄的方法来提供。

io 中也提供了三个和 C 中含义相同的预定义文件句柄:

io.stdin, io.stdout, io.stderr。I/O 库永远不会关闭这些文件。

除非另有说明,I/O 函数在出错时都返回 nil

(第二个返回值为错误消息,第三个返回值为系统相关的错误码)。

成功时返回与 nil 不同的值。

在非 POSIX 系统上,根据错误码取出错误消息的过程可能并非线程安全的,

因为这使用了 C 的全局变量 errno

Member Function Documentation

◆ close()

static function close ( local  file)

等价于 file:close()

不给出 file 时将关闭默认输出文件。

重载
fun()
Parameters
filefile

◆ flush()

static function flush ( )

等价于 io.output():flush()

◆ input()

static function input ( local  file)

用文件名调用它时,(以文本模式)来打开该名字的文件,

并将文件句柄设为默认输入文件。

如果用文件句柄去调用它,

就简单的将该句柄设为默认输入文件。

如果调用时不传参数,它返回当前的默认输入文件。

在出错的情况下,函数抛出错误而不是返回错误码。

重载
fun():number
Parameters
filefile
Returns
number

◆ lines()

static function lines ( local  filename,
local ...   
)

以读模式打开指定的文件名并返回一个迭代函数。

此迭代函数的工作方式和用一个已打开的文件去调用

file:lines(...) 得到的迭代器相同。

当迭代函数检测到文件结束,

它不返回值(让循环结束)并自动关闭文件。

调用 io.lines() (不传文件名)

等价于 io.input():lines("*l")

即,它将按行迭代标准输入文件。

在此情况下,循环结束后它不会关闭文件。

在出错的情况下,函数抛出错误而不是返回错误码。

重载
fun():any
Parameters
filenamestring
Returns
fun():any

◆ open()

static function open ( local  filename,
local  mode 
)

这个函数用字符串 mode 指定的模式打开一个文件。

返回新的文件句柄。

当出错时,返回 nil 加错误消息。

mode 字符串可以是下列任意值:

**"r"** : 读模式(默认);

**"w"** : 写模式;

**"a"** : 追加模式;

**"r+"** : 更新模式,所有之前的数据都保留;

**"w+"** : 更新模式,所有之前的数据都删除;

**"a+"** : 追加更新模式,所有之前的数据都保留,只允许在文件尾部做写入。

mode 字符串可以在最后加一个 'b' ,

这会在某些系统上以二进制方式打开文件。

重载
fun(filename:string):file
Parameters
filenamestring
modestring
Returns
file

◆ output()

static function output ( local  file)

类似于 io.input

不过针对默认输出文件操作。

重载
fun()
Parameters
filefile

◆ popen()

static function popen ( local  prog,
local  mode 
)

这个函数和系统有关,不是所有的平台都提供。

用一个分离进程开启程序 prog

返回的文件句柄可用于从这个程序中读取数据

(如果 mode"r",这是默认值)

或是向这个程序写入输入(当

mode"w" 时)。

重载
fun(prog:file):any
Parameters
progfile
modestring
Returns
any

◆ read()

static function read ( local ...  )

等价于 io.input():read(...)

◆ tmpfile()

static function tmpfile ( )

如果成功,返回一个临时文件的句柄。

这个文件以更新模式打开,在程序结束时会自动删除。

◆ type()

static function type ( local  obj)

检查 obj 是否是合法的文件句柄。

如果 obj 它是一个打开的文件句柄,返回字符串 "file"

如果 obj 是一个关闭的文件句柄,返回字符串 "closed file"

如果 obj 不是文件句柄,返回 nil

Parameters
objstring|file
Returns
file

◆ write()

static function write ( local ...  )

等价于 io.output():write(...)

Member Data Documentation

◆ stderr

file stderr

◆ stdin

file stdin

◆ stdout

file stdout

The documentation for this class was generated from the following files: