Game Development Reference
In-Depth Information
local contents = file:read("*a")
file:close()
print("Contents of the file:", contents)
If this file were opened in binary mode and we needed to get only a particular number of characters,
then we would use it as follows:
local filename = "test.zip"
local file = io.open(filename,"rb")
local contents = file:read(2)
file:close()
print("File Header:", contents) -- All ZIP files have the starting two characters as PK
file:seek ( [whence] [, offset] )
This functions sets and gets the current file position. whence is a string value that can be any of the
following
'set' : The base position 0 (beginning of the file)
'cur' : The current position (default value)
'end' : The end of the file
The offset value passed is measured in bytes from the base specified as per one of these three
options.
file:seek is one of most important functions when working with binary files and fixed formats; it
allows the developer to iterate back and forth to overwrite or update certain portions of the file.
fh = io.tmpfile()
fh:write("Some sample data")
fh:flush()
-- Now let's read the data we just wrote
fh:seek("set", 0)
content = fh:read("*a")
print("We got : ", content)
file:setvbuf (mode [, size] )
This function sets the buffering mode for an output file. There are three buffering modes available in
Lua, and their values are passed as strings:
'no' : No buffering is set, so the output of any operation appears immediately.
'full' : Full buffering is set, so the output operation is only performed when the
buffer is full. You can force a flush to write the buffers.
'line' : Line buffering is set, so the output is buffered until a newline character
(Enter) is found.
io.output():setvbuf("no")
 
Search WWH ::




Custom Search