Difference between revisions of "love.filesystem.write"

(Added note what to do if this function fails)
m (See Also: Added love.filesystem.read.)
 
(12 intermediate revisions by 6 users not shown)
Line 1: Line 1:
Write data to a file
+
Write data to a file in the save directory. If the file existed already, it will be completely replaced by the new contents.
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
success = love.filesystem.write( name, data, size )
+
success, message = love.filesystem.write( name, data, size )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|string|name|The name (and path) of the file}}
+
{{param|string|name|The name (and path) of the file.}}
{{param|string|data|The data that should be written to the file}}
+
{{param|string|data|The string data to write to the file.}}
{{param|number|size (all)|How many bytes to write}}
+
{{param|number|size (all)|How many bytes to write.}}
 
=== Returns ===
 
=== Returns ===
{{param|boolean|success|If the operation was successful}}
+
{{param|boolean|success|If the operation was successful.}}
=== Notes ===
+
{{param|string|message|Error message if operation was unsuccessful.}}
If you are getting the error message "Could not set write directory", try setting the save directory. This is done either with [[love.filesystem.setIdentity]] or by setting the identity field in [[love.conf]] (only available with love 0.7 or higher).
+
 
 +
== Function ==
 +
=== Synopsis ===
 +
<source lang="lua">
 +
success, message = love.filesystem.write( name, data, size )
 +
</source>
 +
=== Arguments ===
 +
{{param|string|name|The name (and path) of the file.}}
 +
{{param|Data|data|The Data object to write to the file.}}
 +
{{param|number|size (all)|How many bytes to write.}}
 +
=== Returns ===
 +
{{param|boolean|success|If the operation was successful.}}
 +
{{param|string|message|Error message if operation was unsuccessful.}}
 +
 
 +
== Example ==
 +
<source lang="lua">
 +
local success, message =love.filesystem.write( filename, "")
 +
if success then
 +
print ('file created')
 +
else
 +
print ('file not created: '..message)
 +
end
 +
</source>
 +
 
 +
 
 +
 
 +
 
 +
== Notes ==
 +
If you are getting the error message "Could not set write directory", try setting the save directory. This is done either with [[love.filesystem.setIdentity]] or by setting the identity field in [[love.conf]].
 +
 
 +
'''Writing to multiple lines''': In Windows, some text editors (e.g. Notepad) only treat CRLF ("\r\n") as a new line.
 +
 
 
== See Also ==
 
== See Also ==
 +
* [[love.filesystem.append]]
 +
* [[love.filesystem.read]]
 
* [[parent::love.filesystem]]
 
* [[parent::love.filesystem]]
 +
 
[[Category:Functions]]
 
[[Category:Functions]]
{{#set:Description=Write data to a file}}
+
{{#set:Description=Write data to a file.}}
 +
{{#set:Since=000}}
 +
 
 +
== Other Languages ==
 +
{{i18n|love.filesystem.write}}

Latest revision as of 10:00, 13 August 2022

Write data to a file in the save directory. If the file existed already, it will be completely replaced by the new contents.

Function

Synopsis

success, message = love.filesystem.write( name, data, size )

Arguments

string name
The name (and path) of the file.
string data
The string data to write to the file.
number size (all)
How many bytes to write.

Returns

boolean success
If the operation was successful.
string message
Error message if operation was unsuccessful.

Function

Synopsis

success, message = love.filesystem.write( name, data, size )

Arguments

string name
The name (and path) of the file.
Data data
The Data object to write to the file.
number size (all)
How many bytes to write.

Returns

boolean success
If the operation was successful.
string message
Error message if operation was unsuccessful.

Example

local success, message =love.filesystem.write( filename, "")
if success then 
	print ('file created')
else 
	print ('file not created: '..message)
end



Notes

If you are getting the error message "Could not set write directory", try setting the save directory. This is done either with love.filesystem.setIdentity or by setting the identity field in love.conf.

Writing to multiple lines: In Windows, some text editors (e.g. Notepad) only treat CRLF ("\r\n") as a new line.

See Also


Other Languages