Difference between revisions of "(File):write"

(Notes)
m (Windows 10 1809 Notepad now treats LF)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Write data to a file
+
Write data to a file.
 +
 
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
success = File:write( data )
+
success, err = File:write( data, size )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|string|data|The data to write}}
+
{{param|string|data|The string data to write.}}
 +
{{param|number|size (all)|How many bytes to write.}}
 
=== Returns ===
 
=== Returns ===
{{param|boolean|success|Whether the operation was successful}}
+
{{param|boolean|success|Whether the operation was successful.}}
=== Notes ===
+
{{param|string|err|The error string if an error occurred.}}
'''Writing to multiple lines''': Use the escape character \n to move to the next line for writing. Some editors may require \r\n in combination to work, your results may vary.
+
 
 +
== Function ==
 +
=== Synopsis ===
 +
<source lang="lua">
 +
success, err = File:write( data, size )
 +
</source>
 +
=== Arguments ===
 +
{{param|Data|data|The Data object to write.}}
 +
{{param|number|size (all)|How many bytes to write.}}
 +
=== Returns ===
 +
{{param|boolean|success|Whether the operation was successful.}}
 +
{{param|string|errorstr|The error string if an error occurred.}}
 +
 
 +
== Notes ==
 +
'''Writing to multiple lines''': In Windows, some text editors (e.g. Notepad before Windows 10 1809) only treat CRLF ("\r\n") as a new line.
 
<source lang="lua">
 
<source lang="lua">
 
--example
 
--example
 
f = love.filesystem.newFile("note.txt")
 
f = love.filesystem.newFile("note.txt")
f:open()
+
f:open("w")
for i = 1, 10 do f:write("This is line "..i.."!\r\n") end
+
for i = 1, 10 do
 +
    f:write("This is line "..i.."!\r\n")
 +
end
 
f:close()
 
f:close()
 
</source>
 
</source>
Line 21: Line 39:
 
== See Also ==
 
== See Also ==
 
* [[parent::File]]
 
* [[parent::File]]
 +
* [[(File):flush|File:flush]]
 +
* [[(File):setBuffer|File:setBuffer]]
 
[[Category:Functions]]
 
[[Category:Functions]]
{{#set:Description=Write data to a file}}
+
{{#set:Description=Write data to a file.}}
 
{{#set:Since=000}}
 
{{#set:Since=000}}
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|(File):write}}
 
{{i18n|(File):write}}

Latest revision as of 03:25, 13 October 2018

Write data to a file.

Function

Synopsis

success, err = File:write( data, size )

Arguments

string data
The string data to write.
number size (all)
How many bytes to write.

Returns

boolean success
Whether the operation was successful.
string err
The error string if an error occurred.

Function

Synopsis

success, err = File:write( data, size )

Arguments

Data data
The Data object to write.
number size (all)
How many bytes to write.

Returns

boolean success
Whether the operation was successful.
string errorstr
The error string if an error occurred.

Notes

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

--example
f = love.filesystem.newFile("note.txt")
f:open("w")
for i = 1, 10 do
    f:write("This is line "..i.."!\r\n")
end
f:close()

See Also


Other Languages