ReadFile
From OxeyeWiki
(Difference between revisions)
(Created page with '{{File}}The ReadFile class will open a binary file for reading. === Example === <pre> local path = daisy.getUserFolderPath("") local file = ReadFile(path .. filename) ...') |
|||
Line 1: | Line 1: | ||
{{File}}The ReadFile class will open a binary file for reading. | {{File}}The ReadFile class will open a binary file for reading. | ||
- | === Example === | + | Call the ReadFile(source, mode) constructor to create a file for reading. The "source" parameter depends on the mode. If the mode is file on disk "f" or compressed file on disk "z", then the source is the file name. If the mode is in memory "m", then the source should be a WriteFile object. |
+ | |||
+ | === Disk Example === | ||
<pre> | <pre> | ||
local path = daisy.getUserFolderPath("") | local path = daisy.getUserFolderPath("") | ||
- | local file = ReadFile(path .. filename) | + | local file = ReadFile(path .. filename, "f") |
if not file:isOk() then | if not file:isOk() then | ||
Line 17: | Line 19: | ||
file:close() | file:close() | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | === In Memory Example === | ||
+ | <pre> | ||
+ | |||
+ | gameData.savedState = WriteFile(nil, "m") -- create a memory file | ||
+ | |||
+ | -- write a bunch of stuff | ||
+ | gameData.savedState:writeInt(value) | ||
+ | |||
+ | -- no need to close the file | ||
+ | |||
+ | -- restore state... | ||
+ | local readState = ReadFile(gameData.savedState, "m") | ||
+ | |||
+ | -- read stuff | ||
+ | value = readState:readInt() | ||
</pre> | </pre> |
Latest revision as of 18:36, 7 February 2010
DaisyMoon File Objects |
---|
Call the ReadFile(source, mode) constructor to create a file for reading. The "source" parameter depends on the mode. If the mode is file on disk "f" or compressed file on disk "z", then the source is the file name. If the mode is in memory "m", then the source should be a WriteFile object.
Disk Example
local path = daisy.getUserFolderPath("") local file = ReadFile(path .. filename, "f") if not file:isOk() then print("Unable to open " .. filename) return end local fileVersion = file:readInt() playerName = file:readString() -- ... and so on ... file:close()
In Memory Example
gameData.savedState = WriteFile(nil, "m") -- create a memory file -- write a bunch of stuff gameData.savedState:writeInt(value) -- no need to close the file -- restore state... local readState = ReadFile(gameData.savedState, "m") -- read stuff value = readState:readInt()