FileCreate: Difference between revisions
Jump to navigation
Jump to search
(New page: file fileCreate ( string filename, resource root = getThisResource () ) Creates a new file. Overwrites with new empty file if already exists.) |
No edit summary |
||
Line 1: | Line 1: | ||
__NOTOC__ | |||
{{Server function}} | |||
Creates a new file. | Creates a new file in a directory of a resource. If there already exists a file with the specified name, it is overwritten with an empty file. | ||
'''Note:''' The file functions should not be used to implement configuration files. It is encouraged to use the XML functions for this instead. | |||
==Syntax== | |||
<syntaxhighlight lang="lua">file fileCreate ( string filename, resource root = getThisResource () )</syntaxhighlight> | |||
===Required arguments=== | |||
*'''filename:''' the name of the file you wish to create. | |||
===Optional arguments=== | |||
*'''root:''' the resource in which the file should be created. Defaults to the resource calling the function. | |||
===Returns=== | |||
If successful, returns a file handle which can be used with other file functions ([[fileWrite]], [[fileClose]]...). Returns ''false'' if an error occured. | |||
==Example== | |||
This example creates a text file and writes a string to it. | |||
<syntaxhighlight lang="lua"> | |||
local newFile = fileCreate("test.txt") -- attempt to create a new file | |||
if newFile then -- check if the creation succeeded | |||
fileWrite(newFile, "This is a test file!") -- write a text line | |||
fileClose(newFile) -- close the file once you're done with it | |||
end | |||
</syntaxhighlight> | |||
It is important to close a file after you've finished all your operations on it, especially if you've been writing to the file. If you don't close a file and your resource crashes, all changes to the file may be lost. |
Revision as of 10:09, 24 November 2007
Creates a new file in a directory of a resource. If there already exists a file with the specified name, it is overwritten with an empty file.
Note: The file functions should not be used to implement configuration files. It is encouraged to use the XML functions for this instead.
Syntax
file fileCreate ( string filename, resource root = getThisResource () )
Required arguments
- filename: the name of the file you wish to create.
Optional arguments
- root: the resource in which the file should be created. Defaults to the resource calling the function.
Returns
If successful, returns a file handle which can be used with other file functions (fileWrite, fileClose...). Returns false if an error occured.
Example
This example creates a text file and writes a string to it.
local newFile = fileCreate("test.txt") -- attempt to create a new file if newFile then -- check if the creation succeeded fileWrite(newFile, "This is a test file!") -- write a text line fileClose(newFile) -- close the file once you're done with it end
It is important to close a file after you've finished all your operations on it, especially if you've been writing to the file. If you don't close a file and your resource crashes, all changes to the file may be lost.