Core Function FileReadBinary

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "<pre> FileReadBinary( <file>, <expression> ) </pre> === Description === Read a number of bytes from a previously opened file starting from current File Steam pointer location....")
 
m (1 revision)
 
(4 intermediate revisions by one user not shown)
Line 23: Line 23:
 
Success: Returns a binary variable filled with data read from file.
 
Success: Returns a binary variable filled with data read from file.
  
Failure: Returns 0 if error occurs.
+
Failure: Returns null if error occurs.
  
 
=== Remarks ===
 
=== Remarks ===
Line 32: Line 32:
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
$File = FileOpen("MyFile.txt", "w")
+
$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
+
If ( isVarObj($File, "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 );
EndIf
+
}
 
+
$File = FileOpen("MyFile.txt", "r")
+
$File = FileOpen("Main.spk", "r");
If isObj($File, "file") Then
+
If ( isVarObj($File, "file") )
$binary = FileReadBinary($File)
+
{
$k = 0
+
$binary = FileReadBinary($File);
For $i In $binary
+
$k = 0;
println( DecPad($k, 5) . " Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) )
+
Foreach ($binary as $i)
$k++
+
{
Next
+
println( DecPad($k, 5) . " Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) );
println( "Info: " . $binary )
+
$k++;
println( "Size: " . BinaryLen($binary) )
+
}
println( "TEXT BELOW\n" . Trim(Unpack("z0", $binary)) . "\nTEXT ABOVE" )
+
println( "Info: " . $binary );
+
println( "Size: " . UBound($binary) );
FileClose( $File )
+
println( "TEXT BELOW\n" . Trim(Unpack("A*", $binary, 3)) . "\nTEXT ABOVE" );
EndIf
+
FileClose( $File );
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 12:38, 14 June 2015

FileReadBinary( <file>, <expression> )

Contents

Description

Read a number of bytes from a previously opened file starting from current File Steam pointer location.

Parameters

file

A variable containing the File handle.

expression

Optional; Number of bytes to read.

If this param is not provided it will read to the end of the file.

Return Value

Success: Returns a binary variable filled with data read from file.

Failure: Returns null if error occurs.

Remarks

N/A

Example

$File = FileOpen("MyFile.txt", "w");
If ( isVarObj($File, "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 );
}
 
$File = FileOpen("Main.spk", "r");
If ( isVarObj($File, "file") )
{
	$binary = FileReadBinary($File);
	$k = 0;
	Foreach ($binary as $i)
	{
		println( DecPad($k, 5) . " Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) );
		$k++;
	}
	println( "Info: " . $binary );
	println( "Size: " . UBound($binary) );
	println( "TEXT BELOW\n" . Trim(Unpack("A*", $binary, 3)) . "\nTEXT ABOVE" ); 
	FileClose( $File );
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox