Truncate a file: Difference between revisions

PascalABC.NET
m (syntax highlighting fixup automation)
(PascalABC.NET)
 
(4 intermediate revisions by 4 users not shown)
Line 578:
 
=={{header|Elena}}==
ELENA 46.x:
<syntaxhighlight lang="elena">import system'io;
import extensions;
Line 597:
{
if (program_arguments.Length != 3)
{ console.printLine:("Please provide the path to the file and a new length"); AbortException.raise() };
auto file := File.assign(program_arguments[1]);
Line 639:
0</syntaxhighlight>
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">
: truncate-file ( fname fnamelen fsize -- )
0 2swap r/w open-file throw
dup >r resize-file throw
r> close-file throw ;
</syntaxhighlight>
=={{header|Fortran}}==
Fortran offers no access to any facilities the file system may offer for truncating a disc file via standard language features, thus in the absence of special routines or deviant compilers that do, you're stuck.
Line 699 ⟶ 706:
CALL FILEHACK("foobar.txt",12)
END</syntaxhighlight>
 
 
=={{header|FreeBASIC}}==
Line 781 ⟶ 787:
}
}</syntaxhighlight>
 
=={{header|jq}}==
jq cannot itself perform the specified task
because it cannot "truncate a file" in the sense of the question.
In particular, it is not "binary safe", and cannot itself write to files named on the command line.
 
Another issue is that jq can only read UTF-8-encoded files, and generally speaking is oriented
to codepoints rather than bytes.
 
So for this task, we'll assume that `sponge` is available for overwriting a file,
and that the "size" is specified in codepoints rather than bytes.
 
<pre>
< input.txt jq -Rr --argjson size $size '
if length < $size then "file size is less than the specified size" | error
else .[:$size]
end
' | sponge input.txt
</pre>
 
 
=={{header|Julia}}==
Line 1,035 ⟶ 1,061:
File "test" truncated at position 3.
</pre>
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
procedure TruncateFile(name: string; length: integer);
begin
var f: file of byte;
Reset(f,name);
if f.Size < length then
raise new System.ArgumentOutOfRangeException('length','The specified length is greater than length of the file');
f.Position := length;
f.Truncate;
f.Close;
end;
 
begin
TruncateFile('aaa.txt',3);
end.
</syntaxhighlight>
 
 
=={{header|Perl}}==
Line 1,549 ⟶ 1,594:
=={{header|Wren}}==
{{libheader|Wren-ioutil}}
<syntaxhighlight lang="ecmascriptwren">import "./ioutil" for FileUtil
 
var fileName = "temp.txt"
205

edits