Truncate a file: Difference between revisions
Content added Content deleted
(added AutoHotkey) |
(Added Bracmat example) |
||
Line 119: | Line 119: | ||
CLOSE #file% |
CLOSE #file% |
||
ENDPROC</lang> |
ENDPROC</lang> |
||
=={{header|Bracmat}}== |
|||
Handling binary data is not easy in Bracmat. This solution reads all bytes as numbers. They are prepended to a list. After reversing the list, all numbers are written to the file. Notice that to close a file you should try to seek to a non-existing file position.<code>fil$(,SET,-1)</code> seeks to the position before the start of the file currently in focus. |
|||
<lang bracmat>( ( trunc |
|||
= name length elif file c |
|||
. !arg:(?name,?length) |
|||
& fil$(!name,rb) |
|||
& fil$(,DEC,1) |
|||
& :?elif |
|||
& whl |
|||
' ( !length+-1:?length:~<0 |
|||
& fil$() !elif:?elif |
|||
) |
|||
& (fil$(,SET,-1)|) |
|||
& whl'(!elif:%?c ?elif&!c !file:?file) |
|||
& fil$(!name,wb) |
|||
& fil$(,DEC,1) |
|||
& whl'(!file:%?c ?file&fil$(,,1,!c)) |
|||
& (fil$(,SET,-1)|) |
|||
& !length:<0 |
|||
) |
|||
& put$("I have a secret to tell you. Listen:","test.txt",NEW) |
|||
& ( trunc$("test.txt",20)&out$(get$("test.txt",STR)) |
|||
| out$"File too short" |
|||
) |
|||
);</lang> |
|||
Output: |
|||
<pre>I have a secret to t</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |