FileClose: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Server function}}
{{Server function}}
Closes a file handled by [[fileCreate]] or [[fileOpen]].
Closes a file handle obtained by [[fileCreate]] or [[fileOpen]].


==Syntax==
==Syntax==
Line 8: Line 8:
</syntaxhighlight>
</syntaxhighlight>


===Required arguments===
===Required Arguments===
*'''theFile:''' the file handle to close.
*'''theFile:''' The file handle to close.


===Returns===
===Returns===
Returns ''true'' if succesfully, ''false'' otherwise.
Returns ''true'' if successful, ''false'' otherwise.


==Example==
==Example==
This example creates a new text file and writes an string in it.
This example creates a text file and writes a string to it.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local newFile = fileCreate("test.txt")             -- Try creating the file.
local newFile = fileCreate("test.txt")               -- attempt to create a new file
if newFile then                                      -- Check if was sucessfully created.
if newFile then                                      -- check if the creation succeeded
     fileWrite(newFile, "This is a test file!") -- Write a text line.
     fileWrite(newFile, "This is a test file!")       -- write a text line
     fileClose(newFile)                                -- Close the file after writing in it.
     fileClose(newFile)                                -- close the file once you're done with it
end
end
</syntaxhighlight>
</syntaxhighlight>


It is important to remember to close the file after done in all operations.
It is important to remember 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.
Especially if you wrote something in it. If you do not close the file and the resource crash all changes are lost.


==See also==
==See Also==
{{File_functions}}
{{File functions}}

Revision as of 07:34, 10 June 2011

Closes a file handle obtained by fileCreate or fileOpen.

Syntax

bool fileClose ( file theFile )

Required Arguments

  • theFile: The file handle to close.

Returns

Returns true if successful, false otherwise.

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 remember 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.

See Also