Truncate a file: Difference between revisions

Line 89:
f.close()
}</lang>
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f TRUNCATE_A_FILE.AWK
BEGIN {
main("NOTHERE",100)
main("FILENAME.TMP",-1)
main("FILENAME.TMP",500)
exit(0)
}
function main(filename,size, ret) {
ret = truncate_file(filename,size)
if (ret != "") {
printf("error: FILENAME=%s, %s\n",filename,ret)
}
}
function truncate_file(filename,size, cmd,fnr,msg,old_BINMODE,old_RS,rec) {
cmd = sprintf("ls --full-time -o %s",filename)
if (size < 0) {
return("size cannot be negative")
}
old_BINMODE = BINMODE
old_RS = RS
BINMODE = 3
RS = "[^\x00-\xFF]"
while (getline rec <filename > 0) {
fnr++
}
close(filename)
if (fnr == 0) {
msg = "file not found"
}
if (fnr > 1) {
msg = "choose a different RecordSeparator"
}
if (msg == "") { # no errors
system(cmd) # optional: show filesize before truncation
if (length(rec) > size) {
rec = substr(rec,1,size)
}
printf("%s",rec) >filename
close(filename)
system(cmd) # optional: show filesize after truncation
}
BINMODE = old_BINMODE
RS = old_RS
return(msg)
}
</lang>
{{out}}
<pre>
error: FILENAME=NOTHERE, file not found
error: FILENAME=FILENAME.TMP, size cannot be negative
-rw-r--r-- 1 1001 240128 Mon Aug 02 12:18:15 2004 FILENAME.TMP
-rw-r--r-- 1 1001 500 Sat Jan 07 00:55:28 2017 FILENAME.TMP
</pre>
 
=={{header|BASIC}}==
477

edits