Core Function FileOpen

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "<pre> FileOpen( <filename>, <flag> ) </pre> === Description === Opens a text file for reading or writing. === Parameters === ==== filename ==== Name of the text file to ope...")
 
 
(13 intermediate revisions by one user not shown)
Line 5: Line 5:
 
=== Description ===
 
=== Description ===
  
Opens a text file for reading or writing.
+
Opens a file for reading or writing.
  
 
=== Parameters ===
 
=== Parameters ===
Line 11: Line 11:
 
==== filename  ====
 
==== filename  ====
  
Name of the text file to open.
+
Name of the file to open (can be text or binary).
  
 
==== flag  ====
 
==== flag  ====
  
 
Mode (read or write) to open the file in options are:
 
Mode (read or write) to open the file in options are:
"" = Open existing file or create a new one with this name
+
 
"a" = Open existing file for appending data to it
+
* "" = Open existing file or create a new one with this name
"w" = Create a new file for writing text to (Will delete an existing file of this name)
+
* "a" = Open existing file for appending data to it
"r" = Open existing file for reading (Will give exception if file does not exist)
+
* "a+" = Open existing file for appending data to it or create a new file in write mode
 +
* "w" = Create a new file for writing to (Will delete an existing file of this name)
 +
* "w+" = Create a new file for writing to (Will NOT delete an existing file of this name)
 +
* "r" = Open existing file for reading (Will give exception if file does not exist)
 +
* "rw" = Open existing file for reading and writing (Will create if the file does not exist)
 +
 
 +
Default: "rw"
  
 
=== Return Value ===
 
=== Return Value ===
  
 
Success: Returns a file "handle" for use with subsequent file functions
 
Success: Returns a file "handle" for use with subsequent file functions
Failure: Returns 0 if error occurs.  
+
 
 +
Failure: Returns null if error occurs.
  
 
=== Remarks ===
 
=== Remarks ===
Line 33: Line 40:
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
 +
$File = FileOpen("MyFile.txt", "w");
 +
If(isVarObj($File, "file")) // You could just use isObj($File) however adding the "file" will make sure it is indeed a file
 +
{
 +
FileAppend( $File, "This is line 1\n" );
 +
FileAppend( $File, "This is line 2\n" );
 +
FileAppend( $File, "This is line 3\n" );
 +
FileClose( $File );
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 10:36, 18 June 2015

FileOpen( <filename>, <flag> )

Contents

Description

Opens a file for reading or writing.

Parameters

filename

Name of the file to open (can be text or binary).

flag

Mode (read or write) to open the file in options are:

Default: "rw"

Return Value

Success: Returns a file "handle" for use with subsequent file functions

Failure: Returns null if error occurs.

Remarks

N/A

Example

$File = FileOpen("MyFile.txt", "w");
If(isVarObj($File, "file")) // You could just use isObj($File) however adding the "file" will make sure it is indeed a file
{
	FileAppend( $File, "This is line 1\n" );
	FileAppend( $File, "This is line 2\n" );
	FileAppend( $File, "This is line 3\n" );
	FileClose( $File );
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox