Difference between revisions of "Bwrite"
From Scriptwiki
m (note) |
m (ascii linked) |
||
Line 13: | Line 13: | ||
[[bread]] myfile.txt 0 [[$file]](myfile.txt).size &test | [[bread]] myfile.txt 0 [[$file]](myfile.txt).size &test | ||
[[echo]] -a [[$bvar]](&test,4,$bvar(&test,0)).text returns ''hi ther'' | [[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. | + | 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 | [[var]] %test = hello | ||
bwrite myfile2.txt -1 -1 %test | bwrite myfile2.txt -1 -1 %test |
Latest revision as of 19: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.