Difference between revisions of "Bwrite"

From Scriptwiki
Jump to: navigation, search
m
 
m (ascii linked)
 
(3 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
If the file doesn't exist, it will be created.
 
If the file doesn't exist, it will be created.
 
If the file is smaller then ''S'' bytes, it will be extended with zeros to the ''S''th byte.
 
If the file is smaller then ''S'' bytes, it will be extended with zeros to the ''S''th byte.
 +
 
If ''S'' = -1 the data will be appended to the end of the file.
 
If ''S'' = -1 the data will be appended to the end of the file.
 +
 
If ''N'' = -1 all of the specified data is written to the file.
 
If ''N'' = -1 all of the specified data is written to the file.
 +
 +
== Examples ==
 +
bwrite myfile.txt 3 7 hi there
 +
[[bread]] myfile.txt 0 [[$file]](myfile.txt).size &test
 +
[[echo]] -a [[$bvar]](&test,4,$bvar(&test,0)).text returns ''hi ther''
 +
If the file "myfile.txt" didn't exist yet, it now contains the [[$asc|ascii]] values: ''0 0 0 104 105 32 116 104 101 114''. That is 3+7=10 bytes.
 +
[[var]] %test = hello
 +
bwrite myfile2.txt -1 -1 %test
 +
We now appended ''hello'' to the file.
 +
 +
== Note ==
 +
Note that if you want to write to the start of the file, posistion 0 is the first byte in the file.
 +
 +
[[Category:Binary Files]]
 +
[[Category:Commands]]

Latest revision as of 20:13, 14 December 2006

The bwrite command is used to write data to a file.

bwrite <filename> <S> [N] <text|%var|&binvar>

This will write N bytes from the specified text / %var / &binvar data to the file, starting at position S. This will overwrite existing data, if any. If the file doesn't exist, it will be created. If the file is smaller then S bytes, it will be extended with zeros to the Sth byte.

If S = -1 the data will be appended to the end of the file.

If N = -1 all of the specified data is written to the file.

Examples

bwrite myfile.txt 3 7 hi there
bread myfile.txt 0 $file(myfile.txt).size &test
echo -a $bvar(&test,4,$bvar(&test,0)).text returns hi ther

If the file "myfile.txt" didn't exist yet, it now contains the ascii values: 0 0 0 104 105 32 116 104 101 114. That is 3+7=10 bytes.

var %test = hello
bwrite myfile2.txt -1 -1 %test

We now appended hello to the file.

Note

Note that if you want to write to the start of the file, posistion 0 is the first byte in the file.