Core Function FileWrite

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Remarks)
Line 5: Line 5:
 
=== Description ===
 
=== Description ===
  
Write text to previously opened text file at the current File Steam pointer location.
+
Write text/binary to previously opened file at the current File Steam pointer location.
  
 
=== Parameters ===
 
=== Parameters ===
Line 16: Line 16:
  
 
Text to write to file.
 
Text to write to file.
 +
 +
OR
 +
 +
A binary variable to write to the file.
  
 
=== Return Value ===
 
=== Return Value ===
Line 31: Line 35:
 
=== Example ===
 
=== Example ===
  
 +
Example with a writing text :
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
 
$File = FileOpen("MyFile.txt", "w")
 
$File = FileOpen("MyFile.txt", "w")
Line 39: Line 44:
 
FileSeek( $File, 0, "B" )
 
FileSeek( $File, 0, "B" )
 
FileWrite( $File, "Replace Line 1" )
 
FileWrite( $File, "Replace Line 1" )
 +
FileClose( $File )
 +
EndIf
 +
</syntaxhighlight>
 +
 +
Example with a writing binary :
 +
 +
<syntaxhighlight lang="sputnik">
 +
$binary = Pack("z0", "Hello World!")
 +
$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
 +
FileWrite( $File, $binary )
 
FileClose( $File )
 
FileClose( $File )
 
EndIf
 
EndIf

Revision as of 13:42, 12 November 2011

FileWrite( <file>, <expression> ) 

Contents

Description

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

Parameters

file

A variable containing the File handle.

expression

Text to write to file.

OR

A binary variable to write to the file.

Return Value

Success: Returns 1

Failure: Returns 0 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

Example with a writing text :

$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

Example with a writing binary :

$binary = Pack("z0", "Hello World!")
$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
	FileWrite( $File, $binary )
	FileClose( $File )
EndIf
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox