Difference between revisions of "love.filesystem.newFile"

m (1 revision: Importing from potato (again).)
(close opened file after use)
 
(7 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 +
Creates a new [[File]] object.
 +
It needs to be opened before it can be accessed.
  
Creates a new File object.
 
It needs to be opened before it can be accessed.
 
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
Line 8: Line 8:
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|string|filename|The filename of the file to read.}}
+
{{param|string|filename|The filename of the file.}}
 
=== Returns ===
 
=== Returns ===
 
{{param|File|file|The new File object.}}
 
{{param|File|file|The new File object.}}
 +
=== Notes ===
 +
Please note that this function will not return any error message (e.g. if you use an invalid filename) because it just creates the File Object. You can still check if the file is valid by using [[(File):open|File:open]] which returns a boolean and an error message if something goes wrong while opening the file.
 +
 +
== Function ==
 +
{{newin|[[0.9.0]]|090|type=variant}}
 +
Creates a [[File]] object and opens it for reading, writing, or appending.
 +
=== Synopsis ===
 +
<source lang="lua">
 +
file, errorstr = love.filesystem.newFile( filename, mode )
 +
</source>
 +
=== Arguments ===
 +
{{param|string|filename|The filename of the file.}}
 +
{{param|FileMode|mode|The mode to open the file in.}}
 +
=== Returns ===
 +
{{param|File|file|The new File object, or nil if an error occurred.}}
 +
{{param|string|errorstr|The error string if an error occurred.}}
 +
 
== Examples ==
 
== Examples ==
 
=== Open a file and read everything ===
 
=== Open a file and read everything ===
 
<source lang="lua">
 
<source lang="lua">
 
file = love.filesystem.newFile("data.txt")
 
file = love.filesystem.newFile("data.txt")
file:open('r')
+
file:open("r")
 
data = file:read()
 
data = file:read()
 +
file:close()
 +
-- use the data ...
 
</source>
 
</source>
 
== See Also ==
 
== See Also ==
 
* [[parent::love.filesystem]]
 
* [[parent::love.filesystem]]
 +
* [[Constructs::File]]
 
[[Category:Functions]]
 
[[Category:Functions]]
{{#set:Description=Creates a new File object.  
+
{{#set:Description=Creates a new [[File]] object.}}
}}
+
{{#set:Since=000}}
 +
== Other Languages ==
 +
{{i18n|love.filesystem.newFile}}

Latest revision as of 14:29, 15 February 2015

Creates a new File object. It needs to be opened before it can be accessed.

Function

Synopsis

file = love.filesystem.newFile( filename )

Arguments

string filename
The filename of the file.

Returns

File file
The new File object.

Notes

Please note that this function will not return any error message (e.g. if you use an invalid filename) because it just creates the File Object. You can still check if the file is valid by using File:open which returns a boolean and an error message if something goes wrong while opening the file.

Function

Available since LÖVE 0.9.0
This variant is not supported in earlier versions.

Creates a File object and opens it for reading, writing, or appending.

Synopsis

file, errorstr = love.filesystem.newFile( filename, mode )

Arguments

string filename
The filename of the file.
FileMode mode
The mode to open the file in.

Returns

File file
The new File object, or nil if an error occurred.
string errorstr
The error string if an error occurred.

Examples

Open a file and read everything

file = love.filesystem.newFile("data.txt")
file:open("r")
data = file:read()
file:close()
-- use the data ...

See Also


Other Languages