Truncate a file: Difference between revisions

→‎{{header|REXX}}: added/changed whitespace and comments, added another REXX version.
m (→‎{{header|REXX}}: adjusted the truncation to the true value.)
(→‎{{header|REXX}}: added/changed whitespace and comments, added another REXX version.)
Line 724:
 
=={{header|REXX}}==
===when there are no memory constraints===
As long as the REXX program has access to enough virtual memory, this version won't have an issue, except for
<br>some versions of REXX that are severely limited in virtual storage by design or by the operating system.
 
Extra boilerplate code was added to ensure that various flavors of REXX correctly handle a file that:
::::* &nbsp; has been read (in input mode),
::::* &nbsp; then erased (deleted), and
::::* &nbsp; then written to.
<lang rexx>/*REXX program truncates a file to a specified (smaller) number of bytes.*/
Line 759 ⟶ 763:
file AA.AA truncated to 333 bytes.
</pre>
 
===with memory constraints===
<lang rexx>/*REXX program truncates a file to a specified (smaller) number of bytes.*/
parse arg siz FID /*get required arguments from the C.L. */
FID=strip(FID) /*elide leading/trailing blanks from FID*/
if siz=='' then call ser "No truncation size was specified (1st argument)."
if FID=='' then call ser "No fileID was specified (2nd argument)."
if \datatype(siz,'W') then call ser "trunc size isn't an integer: " siz
if siz<1 then call ser "trunc size isn't a positive integer: " siz
chunk=4000 /*read the file with this "buffer" size*/
call charin fid,1,0 /*position the file pointer to byte # 1*/
_= /* [↓] read while the length data<siz.*/
do while length(_)<=siz /* [↓] have we reached End-Of-File ? */
buffer=charin(FID,,chunk); if length(buffer)==0 then leave
_=_ || buffer /*append the chunk to the _ input data*/
end /*while ··· */
#=length(_) /*get the length of the part just read.*/
if #==0 then call ser "the specified file doesn't exist: " FID
if #<siz then call ser "the file is smaller than trunc size: " #
call lineout FID /*close the file used, just to be safe.*/
'ERASE' FID /*invoke a command to delete the file */
call lineout FID /*close the file, maybe for REXX's use.*/
call charout FID, left(_,siz), 1 /*write a truncated version of the file*/
call lineout FID /*close the file used, just to be safe.*/
say 'file ' FID " truncated to " siz 'bytes.' /*display some info*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
ser: say '***error!***' arg(1); exit 13 /*display an error message.*/</lang>
'''output''' &nbsp; is identical to the 1<sup>st</sup> REXX version. <br><br>
 
=={{header|Ruby}}==