Core Function FileReadBinary
From Sputnik Wiki
(Difference between revisions)
(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 | + | 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 | + | 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 ); | |
− | + | } | |
− | + | ||
− | $File = FileOpen(" | + | $File = FileOpen("Main.spk", "r"); |
− | If | + | If ( isVarObj($File, "file") ) |
− | $binary = FileReadBinary($File) | + | { |
− | $k = 0 | + | $binary = FileReadBinary($File); |
− | + | $k = 0; | |
− | println( DecPad($k, 5) . " Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) ) | + | Foreach ($binary as $i) |
− | $k++ | + | { |
− | + | println( DecPad($k, 5) . " Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) ); | |
− | println( "Info: " . $binary ) | + | $k++; |
− | println( "Size: " . | + | } |
− | println( "TEXT BELOW\n" . Trim(Unpack(" | + | println( "Info: " . $binary ); |
− | + | println( "Size: " . UBound($binary) ); | |
− | FileClose( $File ) | + | println( "TEXT BELOW\n" . Trim(Unpack("A*", $binary, 3)) . "\nTEXT ABOVE" ); |
− | + | 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 ); }