Core Function FileAppend
From Sputnik Wiki
(Difference between revisions)
(→expression) |
m (1 revision) |
||
(5 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
− | FileAppend( <file>, <expression> ) | + | FileAppend( <file>, <expression>, <useAscii> ) |
</pre> | </pre> | ||
Line 16: | Line 16: | ||
Text to write to the end of the file OR an array $variable to write each element at the end of the file. | Text to write to the end of the file OR an array $variable to write each element at the end of the file. | ||
+ | |||
+ | ==== useAscii ==== | ||
+ | |||
+ | Optional: Flag to decide if ASCII encoding should be used | ||
+ | |||
+ | True = Save using ASCII encoding | ||
+ | |||
+ | False = Save using UNICODE encoding | ||
+ | |||
+ | Default: false (All strings in Sputnik are Unicode to save them to file you must specifically request ASCII encoding) | ||
=== Return Value === | === Return Value === | ||
− | Success: Returns | + | Success: Returns true |
− | Failure: Returns | + | Failure: Returns false if error occurs. |
=== Remarks === | === Remarks === | ||
Line 30: | Line 40: | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | $File = FileOpen("MyFile.txt", "w") | + | $File = FileOpen("MyFile.txt", "w"); |
− | If | + | 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 1\n" ); |
− | FileAppend( $File, "This is line 3\n" ) | + | FileAppend( $File, "This is line 2\n" ); |
− | FileClose( $File ) | + | FileAppend( $File, "This is line 3\n" ); |
− | + | FileClose( $File ); | |
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 42: | Line 53: | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | $lines = array() ; Make a blank array | + | $lines = array(); // Make a blank array |
− | $lines[0] = "This will be line 4\n" | + | $lines[0] = "This will be line 4\n"; |
− | $lines[1] = "This will be line 5\n" | + | $lines[1] = "This will be line 5\n"; |
− | $lines[2] = "This will be line 6\n" | + | $lines[2] = "This will be line 6\n"; |
− | $File = FileOpen("MyFile.txt", "w") | + | $File = FileOpen("MyFile.txt", "w"); |
− | If | + | 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 1\n" ); |
− | FileAppend( $File, "This is line 3\n" ) | + | FileAppend( $File, "This is line 2\n" ); |
− | FileAppend( $File, $lines ) | + | FileAppend( $File, "This is line 3\n" ); |
− | FileClose( $File ) | + | FileAppend( $File, $lines ); |
− | + | FileClose( $File ); | |
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Latest revision as of 12:38, 14 June 2015
FileAppend( <file>, <expression>, <useAscii> )
Contents |
Description
Append a line of text to the end of a previously opened text file.
Parameters
file
A variable containing the File handle.
expression
Text to write to the end of the file OR an array $variable to write each element at the end of the file.
useAscii
Optional: Flag to decide if ASCII encoding should be used
True = Save using ASCII encoding
False = Save using UNICODE encoding
Default: false (All strings in Sputnik are Unicode to save them to file you must specifically request ASCII encoding)
Return Value
Success: Returns true
Failure: Returns false if error occurs.
Remarks
This will always write to the end of the file.
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 ); }
Example of using an array with Append
$lines = array(); // Make a blank array $lines[0] = "This will be line 4\n"; $lines[1] = "This will be line 5\n"; $lines[2] = "This will be line 6\n"; $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" ); FileAppend( $File, $lines ); FileClose( $File ); }