Core Function FileWrite

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
m (1 revision)
 
(6 intermediate revisions by one user not shown)
Line 1: Line 1:
 
<pre>
 
<pre>
FileWrite( <file>, <expression> )  
+
FileWrite( <file>, <expression>, <useAscii> )  
 
</pre>
 
</pre>
  
 
=== Description ===
 
=== Description ===
  
Write text/binary to previously opened file at the current File Steam pointer location.
+
Write text to previously opened text file at the current File Steam pointer location.
  
 
=== Parameters ===
 
=== Parameters ===
Line 17: Line 17:
 
Text to write to file.
 
Text to write to file.
  
OR
+
==== useAscii ====
  
A binary variable to write to the file.
+
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 1
+
Success: Returns true
  
Failure: Returns 0 if error occurs.
+
Failure: Returns false if error occurs.
  
 
=== Remarks ===
 
=== Remarks ===
Line 34: Line 40:
  
 
=== Example ===
 
=== Example ===
 
Example with a writing text :
 
<syntaxhighlight lang="sputnik">
 
$File = FileOpen("MyFile.txt", "w")
 
If isObj($File, "file") Then ; 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" )
 
FileSeek( $File, 0, "B" )
 
FileWrite( $File, "Replace Line 1" )
 
FileClose( $File )
 
EndIf
 
</syntaxhighlight>
 
 
Example with a writing binary :
 
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
$binary = Pack("z0", "Hello World!")
+
$File = FileOpen("MyFile.txt", "w");
$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
If isObj($File, "file") Then ; You could just use isObj($File) however adding the "file" will make sure it is indeed a file
+
{
FileWrite( $File, $binary )
+
FileAppend( $File, "This is line 1\n" );
FileClose( $File )
+
FileAppend( $File, "This is line 2\n" );
EndIf
+
FileAppend( $File, "This is line 3\n" );
 +
FileSeek( $File, 0, "B" );
 +
FileWrite( $File, "Replace Line 1" );
 +
FileClose( $File );
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 12:38, 14 June 2015

FileWrite( <file>, <expression>, <useAscii> ) 

Contents

Description

Write text to previously opened text file at the current File Steam pointer location.

Parameters

file

A variable containing the File handle.

expression

Text to write to 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

You can set the File Steam pointer location with FileSeek( <file>, <offset>, <flag> ).

Warning this will not write to the end of the file unless you tell it to with seek so if you want to write to end of the file you should use FileAppend( <file>, <expression> ) instead.

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" );
	FileSeek( $File, 0, "B" );
	FileWrite( $File, "Replace Line 1" );
	FileClose( $File );
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox