FileWrite: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(New page: number fileWrite ( file, string [, string2, string3 ...]) Write the strings to a the given file at the current position. Returns number of bytes successfully written to the file.)
 
m (Portuguese version indexed)
 
(10 intermediate revisions by 6 users not shown)
Line 1: Line 1:
number fileWrite ( file, string [, string2, string3 ...])
__NOTOC__
{{Server client function}}
Writes one or more strings to a given file, starting at the current read/write position. Advances the position over the number of bytes that were written.


Write the strings to a the given file at the current position. Returns number of bytes successfully written to the file.
==Syntax==
<syntaxhighlight lang="lua">
int fileWrite ( file theFile, string string1 [, string string2, string string3 ...])
</syntaxhighlight>
{{OOP||[[file]]:write}}
 
===Required Arguments===
*'''theFile:''' A handle to the file you wish to write to. The file must have been opened with write access, i.e. the file handle must be a result of [[fileCreate]] or [[fileOpen]] with the readonly parameter set to ''false''.
*'''string1:''' The string to write.
 
===Optional Arguments===
*You can provide any number of additional strings to write after '''string1'''. These will be written in the order in which they are specified.
 
===Returns===
Returns the number of bytes successfully written to the file, returns ''false'' if invalid arguments were specified.
 
==Example==
This example creates a text file and writes a string to it.
<syntaxhighlight lang="lua">
local fileHandle = fileCreate("test.txt")            -- attempt to create a new file
if fileHandle then                                    -- check if the creation succeeded
    fileWrite(fileHandle, "This is a test file!")    -- write a text line
    fileClose(fileHandle)                            -- close the file once you're done with it
end
</syntaxhighlight>
Notice that you can't simply do fileWrite("test.txt", "File content"). Instead, file functions operate on a '''file handle''', which is a special object representing an open file.
 
It is also 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==
{{File functions}}
[[pt-br:fileWrite]]

Latest revision as of 20:15, 20 December 2023

Writes one or more strings to a given file, starting at the current read/write position. Advances the position over the number of bytes that were written.

Syntax

int fileWrite ( file theFile, string string1 [, string string2, string string3 ...])

OOP Syntax Help! I don't understand this!

Method: file:write(...)


Required Arguments

  • theFile: A handle to the file you wish to write to. The file must have been opened with write access, i.e. the file handle must be a result of fileCreate or fileOpen with the readonly parameter set to false.
  • string1: The string to write.

Optional Arguments

  • You can provide any number of additional strings to write after string1. These will be written in the order in which they are specified.

Returns

Returns the number of bytes successfully written to the file, returns false if invalid arguments were specified.

Example

This example creates a text file and writes a string to it.

local fileHandle = fileCreate("test.txt")             -- attempt to create a new file
if fileHandle then                                    -- check if the creation succeeded
    fileWrite(fileHandle, "This is a test file!")     -- write a text line
    fileClose(fileHandle)                             -- close the file once you're done with it
end

Notice that you can't simply do fileWrite("test.txt", "File content"). Instead, file functions operate on a file handle, which is a special object representing an open file.

It is also 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