Truncate a file: Difference between revisions
PascalABC.NET
(Truncate a file en FreeBASIC) |
(PascalABC.NET) |
||
(7 intermediate revisions by 7 users not shown) | |||
Line 18:
{{trans|Python}}
<
I !fs:is_file(name)
R 0B
Line 24:
R 0B
fs:resize_file(name, length)
R 1B</
=={{header|Action!}}==
The attached result has been obtained under DOS 2.5.
{{libheader|Action! Tool Kit}}
<
PROC Dir(CHAR ARRAY filter)
Line 130:
PrintF("Dir ""%S""%E",filter)
Dir(filter)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Truncate_a_file.png Screenshot from Atari 8-bit computer]
Line 153:
The following program is an implementation in Ada using system-independent tools from the standard library to read and write files, remove and rename them. It should work for on any system with byte-oriented file storage and uses Ada 2012 conditional expressions.
<
procedure Truncate_File is
Line 207:
end if;
end;
end Truncate_File;</
=={{header|AutoHotkey}}==
<
return
Line 222:
f.length := length_bytes
f.close()
}</
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f TRUNCATE_A_FILE.AWK
BEGIN {
Line 271:
return(msg)
}
</syntaxhighlight>
{{out}}
<pre>
Line 285:
This is fairly generic MS BASIC. As such, it actually works in a wide variety of compilers and interpreters -- certainly too many to list here, but the list includes non-.Net [[Visual Basic]], [[FreeBASIC]], [[QB64]], etc. With a user-provided implementation of <code>DIR$</code>, it even works under [[QBasic]].
<
IF LEN(DIR$(file)) THEN
DIM f AS LONG, c AS STRING
Line 304:
ERROR 53
END IF
END SUB</
See also: [[#FreeBASIC|FreeBASIC]], [[#Liberty BASIC|Liberty BASIC]], [[#PowerBASIC|PowerBASIC]], [[#PureBasic|PureBasic]], [[#ZX Spectrum Basic|ZX Spectrum Basic]].
Line 310:
=={{header|BBC BASIC}}==
This will extend the file if the specified length is greater than the existing length. A test to prevent that could easily be added.
<
LOCAL file%
file% = OPENUP(file$)
Line 316:
EXT#file% = size%
CLOSE #file%
ENDPROC</
=={{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.
<
= name length elif file c
. !arg:(?name,?length)
Line 342:
| out$"File too short"
)
);</
Output:
<pre>I have a secret to t</pre>
Line 355:
{{works with|MinGW}}
<
#include <stdio.h>
#include <wchar.h>
Line 435:
return dotruncate(fn, fp);
}</
===POSIX===
<
#include <sys/types.h>
Line 445:
ftruncate(fd, length);
...
</syntaxhighlight>
Both functions have <code>length</code> argument of <code>off_t</code> type. There are about a million possible errors, of interest to this task are <code>EFBIG</code>: size too large; <code>EINVAL</code>: size is negative or too large; <code>EIO</code>: IO error; <code>EACCESS</code> (for <code>truncate</code>): either can't see or can't write to the file; <code>EBADF</code> or <code>EINVAL</code> (for <code>ftruncate</code>): file descriptor is not a file descriptor, or not opened for writing.
Line 452:
=={{header|C sharp}}==
<
using System.IO;
Line 479:
}
}
}</
=={{header|C++}}==
<
#include <fstream>
Line 502:
return 0;
}
</syntaxhighlight>
=={{header|Clojure}}==
{{trans|Java}}
<
(with-open [chan (.getChannel (java.io.FileOutputStream. file true))]
(.truncate chan size)))
(truncate "truncate_test.txt" 2)</
=={{header|D}}==
<
void truncateFile(in string name, in size_t newSize) {
Line 534:
void main() {
truncateFile("truncate_test.txt", 0);
}</
=={{header|Delphi}}==
Line 540:
Delphi has the same <code>truncate</code> method as Pascal, which could be used like this:
<
var
aFile: file of byte;
Line 552:
Close(afile);
end;
end;</
Most people nowadays seem to use streams to access files, so an alternative is:
<
var
Stream : TFileStream;
Line 566:
Stream.Free;
end;
end;</
With both of these approaches, if the NewSize is greater than the existing file size the file will be expanded and filled with nulls.
Line 578:
=={{header|Elena}}==
ELENA
<
import extensions;
Line 597:
{
if (program_arguments.Length != 3)
{ console.printLine
auto file := File.assign(program_arguments[1]);
Line 606:
file.Length := length
}</
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
-module( truncate ).
Line 621:
{ok, Size} = file:position( IO, {bof, Size} ),
ok = file:truncate( IO ).
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
{{trans|C#}}
<
open System.IO
Line 637:
let main args =
truncateFile args.[0] (Int64.Parse(args.[1]))
0</
=={{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 653 ⟶ 660:
There is an option to specify BUFFERED="YES" and BUFFERCOUNT=n or similar in many Fortrans, but alas, these are not standard even in F90/95. One's guilt is not so easily diverted, but, ... this is really only a demonstration, perhaps with very occasional use.
And so...<
CHARACTER*(*) GASP !As noted.
WRITE (6,*) "Oh dear. ",GASP !So, gasp away.
Line 698 ⟶ 705:
PROGRAM CHOPPER
CALL FILEHACK("foobar.txt",12)
END</
=={{header|FreeBASIC}}==
{{trans|QuickBASIC}}
<
If Len(Dir(archivo)) Then
Dim f As Long, c As String
Line 722 ⟶ 728:
Error 53 'File not found
End If
End Sub</
=={{header|Go}}==
Go has the required function in the standard library. The implementation calls operating system specific functions and returns whatever errors the operating system reports.
<
"fmt"
"os"
Line 733 ⟶ 739:
if err := os.Truncate("filename", newSize); err != nil {
fmt.Println(err)
}</
Package os also has a separate function that operates on an open file.
Line 739 ⟶ 745:
This can be achieved by using the <code>setFileSize</code> function in [http://hackage.haskell.org/packages/archive/unix-compat/0.1.2.1/doc/html/System-PosixCompat-Files.html#13 System.PosixCompat.Files]:
<
-- Truncates the file down to the specified length.
-- If the file was larger than the given length
-- before this operation was performed the extra is lost.
-- Note: calls truncate.
</syntaxhighlight>
=={{header|Icon}} and {{header|Unicon}}==
Unicon provides the built-in function truncate which can be used to truncate a file. The following line of code truncates ''filename'' to ''newsizeinbytes''. The file is opened for both read and write in untranslated mode.
<
Note: The Unicon book incorrectly indicates that truncate doesn't work on Windows.
=={{header|J}}==
'''Solution:'''
<
ftruncate=: ] fwrite~ ] fread@; 0 , [</
'''Usage:'''
<
567 ftruncate 'test.txt' NB. truncate test file
567
fsize 'test.txt' NB. check new file size
567</
=={{header|Java}}==
The built-in function for truncating a file in Java will leave the file unchanged if the specified size is larger than the file. This version expects the source file name and the new size as command line arguments (in that order).
<
import java.io.IOException;
import java.nio.channels.FileChannel;
Line 780 ⟶ 786:
outChan.close();
}
}</
=={{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}}==
Uses the built-in <code>truncate</code> function (which truncates a stream):
<
open(fname, "r+") do f
truncate(f, size)
end
end</
=={{header|Kotlin}}==
<
import java.io.FileOutputStream
Line 815 ⟶ 841:
fun main(args: Array<String>) {
truncateFile("test.txt", 10)
}</
=={{header|Lasso}}==
<
local(file = file(#path))
Line 836 ⟶ 862:
stdoutnl(file(#filepath) -> readbytes)
stdout(file('Truncated size: ' + #filepath) -> size)</
Output:
<pre>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris consequat
Line 859 ⟶ 885:
<br>
It is also possible to call API functions to achieve this task.
<syntaxhighlight lang="lb">
dim info$( 50, 50) ' NB pre-dimension before calling file-exists
' needed for file-exists function
Line 890 ⟶ 916:
end
</syntaxhighlight>
=={{header|Lingo}}==
With native means (Fileio Xtra) file truncation can only be implemented indirectly, either using a temp file or loading truncated file contents into memory, deleting original and writing memory back to file:
<
-- Truncates file
-- @param {string} filename
Line 921 ⟶ 947:
fp.closeFile()
return ok
end</
But there are also free plugins ("Xtras") like e.g. "BinFile Xtra" that support "in-file" truncation:
{{libheader|BinFile Xtra}}
<
bx_file_truncate(_movie.path&"foo.dat", 10240)</
=={{header|Lua}}==
Lua treats strings as being invariably one byte per character (hence the awkwardness of trying to use it with unicode), so it's safe to use string methods to truncate binary data.
<
local inFile = io.open(filename, 'r')
if not inFile then
Line 942 ⟶ 968:
outFile:write(wholeFile:sub(1, length))
outFile:close()
end</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
temp = $TemporaryPrefix <> filename;
BinaryWrite[temp, BinaryReadList[filename, "Byte", nbbytes]];
Close[temp]; DeleteFile[filename]; RenameFile[temp, filename];
]</
=={{header|MATLAB}} / {{header|Octave}}==
<
fid=fopen(fn,'r');
Line 960 ⟶ 986:
fid=fopen(fn,'w');
s = fwrite(fid,s,'uint8');
fclose(fid); </
=={{header|Nim}}==
Line 967 ⟶ 993:
The function returns a code which can be checked to emit a message or raise an exception in case of error. In the following example, we ignore (discard) this return code.
<
discard truncate("filename", 1024)</
=={{header|OCaml}}==
Line 975 ⟶ 1,001:
The <code>Unix</code> module provided with the standard distribution provides a function <code>[http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#VALtruncate truncate]</code>:
<
(** Truncates the named file to the given size. *)</
There is also a function <code>[http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#VALftruncate ftruncate]</code> that does the equivalent operation but with a file descriptor instead of a file name:
<
(** Truncates the file corresponding to the given descriptor to the given size. *)</
=={{header|PARI/GP}}==
Create a file /tmp/test.file and truncate to 20 bytes: (Linux only)
<
trunc("/tmp/test.file", 20)</
=={{header|Pascal}}==
<
Program FileTruncate;
Line 1,028 ⟶ 1,054:
writeln('File "', filename, '" truncated at position ', position, '.');
end.
</syntaxhighlight>
Output:
<pre>
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}}==
<
open FOO, ">>file" or die;
truncate(FOO, 1234);
Line 1,043 ⟶ 1,088:
# Truncate a file to 567 bytes.
truncate("file", 567);</
=={{header|Phix}}==
In honour of this very task, there is now (0.8.0+) a set_file_size() builtin, for the grubby/cross-platform details see builtins/pfile.e (an autoinclude)<br>
It will pad or truncate as needed.
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">get_file_size</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"test.txt"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">set_file_size</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"test.txt"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">get_file_size</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"test.txt"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">set_file_size</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"test.txt"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1024</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">get_file_size</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"test.txt"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}} (annotated, repeatable, assumes test.txt already exists)
<pre>
Line 1,064 ⟶ 1,112:
=={{header|PicoLisp}}==
On the 64-bit version, we can call the native runtime library:
<
(native "@" "truncate" 'I File Len) )</
Otherwise (on all versions), we call the external truncate command:
<
(call "truncate" "-s" Len File) )</
=={{header|PL/I}}==
<syntaxhighlight lang="text">
/* Parameters to be read in by the program are: */
/* 1. the name of the file to be truncated, and */
Line 1,100 ⟶ 1,148:
end;
end truncate_file;
</syntaxhighlight>
=={{header|PowerBASIC}}==
Line 1,107 ⟶ 1,155:
While PowerBASIC can use the QuickBASIC version of <code>truncateFile</code>, PB provides an easier way to do this, via the <code>SETEOF</code> function -- but since <code>SETEOF</code> will extend a file if it is not as long as specified, we still need to wrap it in a <code>SUB</code> to meet this task's specifications.
<
IF LEN(DIR$(file)) THEN
DIM f AS LONG
Line 1,123 ⟶ 1,171:
ERROR 53 'File not found
END IF
END SUB</
=={{header|Powershell}}==
<
$null | Set-Content -Path "$fname"
}
</syntaxhighlight>
=={{header|PureBasic}}==
PureBasic has the internal function [http://www.purebasic.com/documentation/file/truncatefile.html TruncateFile] that cuts the file at the current file position and discards all data that follows.
<
Protected fh, pos, i
If FileSize(File$) < length
Line 1,145 ⟶ 1,193:
EndIf
ProcedureReturn #True
EndProcedure</
=={{header|Python}}==
<
def truncate_file(name, length):
if not os.path.isfile(name):
Line 1,157 ⟶ 1,205:
f.truncate(length)
return True
</syntaxhighlight>
=={{header|R}}==
<syntaxhighlight lang="r">
truncate_file <- function(filename, n_bytes) {
Line 1,189 ⟶ 1,237:
}
</syntaxhighlight>
=={{header|Racket}}==
Line 1,195 ⟶ 1,243:
Racket has a <tt>file-truncate</tt> function that expects an open port and truncate its associated file. Note that it can also extend the file, and the code below prints a warning in that case.
<syntaxhighlight lang="racket">
#lang racket
Line 1,203 ⟶ 1,251:
(call-with-output-file* file #:exists 'update
(λ(o) (file-truncate o size))))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2016.07}}
<syntaxhighlight lang="raku"
sub truncate(Str, int32 --> int32) is native {*}
Line 1,219 ⟶ 1,267:
}
truncate($file, $to) == 0 or die "Truncation was unsuccessful";
}</
The external <code>truncate</code> routine could be replaced with the following line (in which case no need for <code>NativeCall</code>):
<syntaxhighlight lang="raku"
=={{header|REXX}}==
Line 1,233 ⟶ 1,281:
::::* then erased (deleted),
::::* then written to.
<
parse arg siz FID /*obtain required arguments from the CL*/
FID=strip(FID) /*elide FID leading/trailing blanks. */
Line 1,252 ⟶ 1,300:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
ser: say '***error***' arg(1); exit 13 /*display an error message and exit. */</
For this example, Windows/XP and Windows 7 operating systems were used, the program was tested with:
:::* Personal REXX
Line 1,266 ⟶ 1,314:
===with memory constraints===
<
parse arg siz FID /*obtain required arguments from the CL*/
FID=strip(FID) /*elide FID leading/trailing blanks. */
Line 1,292 ⟶ 1,340:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
ser: say '***error***' arg(1); exit 13 /*display an error message and exit. */</
'''output''' is identical to the 1<sup>st</sup> REXX version. <br><br>
=={{header|Ring}}==
<
file = "C:\Ring\ReadMe.txt"
fp = read(file)
Line 1,302 ⟶ 1,350:
see fpstr + nl
write(file, fpstr)
</syntaxhighlight>
Output:
<pre>
Line 1,314 ⟶ 1,362:
This only works with some platforms. If truncation is not available, then Ruby raises NotImplementedError.
<
File.open("file", "ab") do |f|
f.truncate(1234)
Line 1,321 ⟶ 1,369:
# Just truncate a file to 567 bytes.
File.truncate("file", 567)</
=={{header|Rust}}==
<
use std::fs;
Line 1,347 ⟶ 1,395:
/// Likely due to having read but not write permissions
UnableToWrite,
}</
=={{header|Scala}}==
{{libheader|Scala}}<
object TruncFile extends App {
Line 1,359 ⟶ 1,407:
outChan.truncate(newSize)
}
}</
=={{header|Sidef}}==
<
var file = File(filename);
len > file.size ->
Line 1,370 ⟶ 1,418:
# truncate "file.ext" to 1234 bytes
truncate("file.ext", 1234);</
=={{header|Standard ML}}==
{{works with|Unix}}
This function behaves similarly to the ''truncate'' tool from the [[GNU]] coreutils: If the file does not exist yet, or the target length is larger than the current file size, the extended area appears zero-filled (usually resulting in a sparse file).
<
open Posix.FileSys
val perm = S.flags [S.irusr, S.iwusr, S.irgrp, S.iwgrp, S.iroth, S.iwoth]
Line 1,385 ⟶ 1,433:
ftruncate (fd, len); Posix.IO.close fd
end
end</
=={{header|Tcl}}==
<
set f [open "file" r+]; # Truncation is done on channels
chan truncate $f 1234; # Truncate at a particular length (in bytes)
close $f</
=={{header|UNIX Shell}}==
The dd(1) command can truncate a file. Because dd(1) would create the file, this example runs ls(1). If the file does not exist, then ls(1) prints an error. If the file exists, then dd(1) truncates the file or prints an error. Unix can extend a file, so there is no error if the length increases.
<
ls myfile >/dev/null &&
dd if=/dev/null of=myfile bs=1 seek=1440k</
----
Some systems have a truncate(1) command ([http://www.freebsd.org/cgi/man.cgi?query=truncate&apropos=0&sektion=0&manpath=FreeBSD+8.2-RELEASE&format=html FreeBSD truncate(1)], [http://www.gnu.org/software/coreutils/manual/html_node/truncate-invocation.html#truncate-invocation GNU truncate(1)]).
<
truncate -s 1440k myfile</
----
Pure bourne-shell approach (cross-OS, no truncate(1) binary required)
<
# 1. simplest one-liner
-bash$ >file
Line 1,497 ⟶ 1,545:
abc+ echo _EOF_
_EOF_
</syntaxhighlight>
NOTE[4]: Designed for regular text files. Does not work when files contain: chars 0x00 and 0xFF as of bash 4.2 - these chars are treated differently in read -N. For this reason use of method#4 is not prefer over dd(1) or truncate(1) when using binary files, or large files. [4b] http://unix.stackexchange.com/questions/8618/script-that-keep-reading-a-stream,[4a] https://groups.google.com/forum/#!topic/gnu.bash.bug/a2rjQHpQYSU
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Sub truncate(fpath,n)
'Check if file exist
Line 1,542 ⟶ 1,590:
'testing
Call truncate("C:\temp\test.txt",30)
</syntaxhighlight>
=={{header|Wren}}==
{{libheader|Wren-ioutil}}
<
var fileName = "temp.txt"
Line 1,560 ⟶ 1,608:
// attempt to truncate file to 20 bytes
FileUtil.truncate(fileName, 20)
System.print("Contents are still : %(FileUtil.read(fileName))")</
{{out}}
Line 1,568 ⟶ 1,616:
Contents are still : abcdefghijklm
</pre>
=={{header|XPL0}}==
Works for binary files.
<syntaxhighlight lang="xpl0">int I, Size, FD;
char C, FN(80), Array;
[I:= 0; \get file name from command line
loop [C:= ChIn(8);
if C = $20 \space\ then quit;
FN(I):= C;
I:= I+1;
];
FN(I):= 0;
Size:= IntIn(8); \get number of bytes from command line
if Size = 0 then [Text(0, "Length not found (or zero)"); exit 1];
Trap(false); \disable abort on errors
FD:= FOpen(FN, 0); \open specified file for input
FSet(FD, ^i);
OpenI(3);
if GetErr then [Text(0, "File not found"); exit 1];
Array:= Reserve(0); \64MB available if no procedures are called
for I:= 0 to Size-1 do \read specified number of bytes
[Array(I):= ChIn(3);
if GetErr then [Text(0, "File is too short"); exit 1];
]; \if end of file encountered, show error
FClose(FD);
FD:= FOpen(FN, 1); \open file by same name for output
FSet(FD, ^o);
OpenO(3);
if GetErr then [Text(0, "Output error"); exit 1];
for I:= 0 to Size-1 do ChOut(3, Array(I));
Close(3);
]</syntaxhighlight>
=={{header|ZX Spectrum Basic}}==
Line 1,573 ⟶ 1,656:
We can truncate files that were saved as binary. We don't know the length of the original file, so if the provided length is longer, then the file will be extended.
<
20 INPUT "Which file do you want to truncate?";f$
30 PRINT "Start tape to load file to truncate."
Line 1,580 ⟶ 1,663:
60 PRINT "Please rewind the tape and press record."
70 SAVE f$ CODE 30000,n
80 STOP</
{{omit from|GUISS}}
|