Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
4
Task/Truncate-a-file/00-META.yaml
Normal file
4
Task/Truncate-a-file/00-META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- File System Operations
|
||||
from: http://rosettacode.org/wiki/Truncate_a_file
|
||||
13
Task/Truncate-a-file/00-TASK.txt
Normal file
13
Task/Truncate-a-file/00-TASK.txt
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
;Task:
|
||||
Truncate a file to a specific length. This should be implemented as a routine that takes two parameters: the filename and the required file length (in bytes).
|
||||
|
||||
|
||||
Truncation can be achieved using system or library calls intended for such a task, if such methods exist, or by creating a temporary file of a reduced size and renaming it, after first deleting the original file, if no other method is available. The file may contain non human readable binary data in an unspecified format, so the routine should be "binary safe", leaving the contents of the untruncated part of the file unchanged.
|
||||
|
||||
If the specified filename does not exist, or the provided length is not less than the current file length, then the routine should raise an appropriate error condition.
|
||||
|
||||
On some systems, the provided file truncation facilities might not change the file or may extend the file, if the specified length is greater than the current length of the file.
|
||||
|
||||
This task permits the use of such facilities. However, such behaviour should be noted, or optionally a warning message relating to an non change or increase in file size may be implemented.
|
||||
<br><br>
|
||||
|
||||
7
Task/Truncate-a-file/11l/truncate-a-file.11l
Normal file
7
Task/Truncate-a-file/11l/truncate-a-file.11l
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
F truncate_file(name, length)
|
||||
I !fs:is_file(name)
|
||||
R 0B
|
||||
I length >= fs:file_size(name)
|
||||
R 0B
|
||||
fs:resize_file(name, length)
|
||||
R 1B
|
||||
45
Task/Truncate-a-file/AWK/truncate-a-file.awk
Normal file
45
Task/Truncate-a-file/AWK/truncate-a-file.awk
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# 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)
|
||||
}
|
||||
102
Task/Truncate-a-file/Action-/truncate-a-file.action
Normal file
102
Task/Truncate-a-file/Action-/truncate-a-file.action
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
INCLUDE "D2:IO.ACT" ;from the Action! Tool Kit
|
||||
|
||||
PROC Dir(CHAR ARRAY filter)
|
||||
BYTE dev=[1]
|
||||
CHAR ARRAY line(255)
|
||||
|
||||
Close(dev)
|
||||
Open(dev,filter,6)
|
||||
DO
|
||||
InputSD(dev,line)
|
||||
PrintE(line)
|
||||
IF line(0)=0 THEN
|
||||
EXIT
|
||||
FI
|
||||
OD
|
||||
Close(dev)
|
||||
RETURN
|
||||
|
||||
BYTE FUNC Find(CHAR ARRAY text CHAR c)
|
||||
BYTE i
|
||||
|
||||
i=1
|
||||
WHILE i<=text(0)
|
||||
DO
|
||||
IF text(i)=c THEN
|
||||
RETURN (i)
|
||||
FI
|
||||
i==+1
|
||||
OD
|
||||
RETURN (0)
|
||||
|
||||
PROC MakeRenameCommand(CHAR ARRAY left,right,result)
|
||||
BYTE i,len,pos
|
||||
|
||||
SCopy(result,left)
|
||||
len=left(0)+1
|
||||
result(len)=32
|
||||
pos=Find(right,':)
|
||||
FOR i=pos+1 TO right(0)
|
||||
DO
|
||||
len==+1
|
||||
result(len)=right(i)
|
||||
OD
|
||||
result(0)=len
|
||||
RETURN
|
||||
|
||||
PROC DeleteFile(CHAR ARRAY fname)
|
||||
BYTE dev=[1]
|
||||
|
||||
Close(dev)
|
||||
Xio(dev,0,33,0,0,fname)
|
||||
RETURN
|
||||
|
||||
PROC TruncateFile(CHAR ARRAY fname CARD maxlen)
|
||||
DEFINE BUF_LEN="100"
|
||||
CHAR ARRAY tmp="D:TEMP.TMP",cmd(255)
|
||||
BYTE in=[1], out=[2]
|
||||
BYTE ARRAY buff(BUF_LEN)
|
||||
CARD len,size
|
||||
|
||||
Close(in)
|
||||
Close(out)
|
||||
Open(in,fname,4)
|
||||
Open(out,tmp,8)
|
||||
|
||||
DO
|
||||
IF maxlen>BUF_LEN THEN
|
||||
size=BUF_LEN
|
||||
ELSE
|
||||
size=maxlen
|
||||
FI
|
||||
len=Bget(in,buff,size)
|
||||
maxlen==-len
|
||||
IF len>0 THEN
|
||||
Bput(out,buff,len)
|
||||
FI
|
||||
UNTIL len#BUF_LEN
|
||||
OD
|
||||
|
||||
Close(in)
|
||||
Close(out)
|
||||
|
||||
MakeRenameCommand(tmp,fname,cmd)
|
||||
DeleteFile(fname)
|
||||
Rename(cmd)
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
CHAR ARRAY filter="D:*.*", fname="D:INPUT.TXT"
|
||||
CARD len=[400]
|
||||
|
||||
Put(125) PutE() ;clear screen
|
||||
|
||||
PrintF("Dir ""%S""%E",filter)
|
||||
Dir(filter)
|
||||
|
||||
PrintF("Truncate ""%S"" to %U bytes.%E%E",fname,len)
|
||||
TruncateFile(fname,len)
|
||||
|
||||
PrintF("Dir ""%S""%E",filter)
|
||||
Dir(filter)
|
||||
RETURN
|
||||
55
Task/Truncate-a-file/Ada/truncate-a-file.ada
Normal file
55
Task/Truncate-a-file/Ada/truncate-a-file.ada
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
with Ada.Command_Line, Ada.Sequential_IO, Ada.Directories;
|
||||
|
||||
procedure Truncate_File is
|
||||
|
||||
type Byte is mod 256;
|
||||
for Byte'Size use 8;
|
||||
|
||||
package Byte_IO is new Ada.Sequential_IO(Byte);
|
||||
|
||||
function Arg(N: Positive) return String renames Ada.Command_Line.Argument;
|
||||
function Args return Natural renames Ada.Command_Line.Argument_Count;
|
||||
|
||||
begin
|
||||
-- give help output if neccessary
|
||||
if Args < 2 or else Args > 3 then
|
||||
raise Program_Error
|
||||
with "usage: truncate_file <filename> <length> [<temp_file>]";
|
||||
end if;
|
||||
|
||||
-- now do the real work
|
||||
declare
|
||||
File_Name: String := Arg(1);
|
||||
Temp_Name: String := (if Args = 2 then Arg(1) & ".tmp" else Arg(3));
|
||||
-- an Ada 2012 conditional expression
|
||||
File, Temp: Byte_IO.File_Type;
|
||||
Count: Natural := Natural'Value(Arg(2));
|
||||
Value: Byte;
|
||||
begin
|
||||
-- open files
|
||||
Byte_IO.Open (File => File, Mode => Byte_IO.In_File, Name => File_Name);
|
||||
Byte_IO.Create(File => Temp, Mode => Byte_IO.Out_File, Name => Temp_Name);
|
||||
|
||||
-- copy the required bytes (but at most as much as File has) from File to Temp
|
||||
while (not Byte_IO.End_Of_File(File)) and Count > 0 loop
|
||||
Byte_IO.Read (File, Value);
|
||||
Byte_IO.Write(Temp, Value);
|
||||
Count := Count - 1;
|
||||
end loop;
|
||||
|
||||
-- close files
|
||||
Byte_IO.Close(Temp);
|
||||
Byte_IO.Close(File);
|
||||
|
||||
if Count = 0 then -- File was at least Count bytes long
|
||||
-- remove File and rename Temp to File
|
||||
Ada.Directories.Delete_File(Name => File_Name);
|
||||
Ada.Directories.Rename(Old_Name => Temp_Name, New_Name => File_Name);
|
||||
else -- File was too short
|
||||
-- remove Temp and leave File as it is, output error
|
||||
Ada.Directories.Delete_File(Name => Temp_Name);
|
||||
raise Program_Error
|
||||
with "Size of """ & File_Name & """ less than " & Arg(2);
|
||||
end if;
|
||||
end;
|
||||
end Truncate_File;
|
||||
13
Task/Truncate-a-file/AutoHotkey/truncate-a-file.ahk
Normal file
13
Task/Truncate-a-file/AutoHotkey/truncate-a-file.ahk
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
truncFile("S:\Portables\AutoHotkey\My Scripts\Other_Codes\motion2.ahk", 1200)
|
||||
return
|
||||
|
||||
truncFile(file, length_bytes){
|
||||
if !FileExist(file)
|
||||
msgbox, File doesn't exists.
|
||||
FileGetSize, fsize, % file, B
|
||||
if (length_bytes>fsize)
|
||||
msgbox, New truncated size more than current file size
|
||||
f := FileOpen(file, "rw")
|
||||
f.length := length_bytes
|
||||
f.close()
|
||||
}
|
||||
20
Task/Truncate-a-file/BASIC/truncate-a-file.basic
Normal file
20
Task/Truncate-a-file/BASIC/truncate-a-file.basic
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
SUB truncateFile (file AS STRING, length AS LONG)
|
||||
IF LEN(DIR$(file)) THEN
|
||||
DIM f AS LONG, c AS STRING
|
||||
f = FREEFILE
|
||||
OPEN file FOR BINARY AS f
|
||||
IF length > LOF(f) THEN
|
||||
CLOSE f
|
||||
ERROR 62 'Input past end of file
|
||||
ELSE
|
||||
c = SPACE$(length)
|
||||
GET #f, 1, c
|
||||
CLOSE f
|
||||
OPEN file FOR OUTPUT AS f
|
||||
PRINT #f, c;
|
||||
CLOSE f
|
||||
END IF
|
||||
ELSE
|
||||
ERROR 53
|
||||
END IF
|
||||
END SUB
|
||||
7
Task/Truncate-a-file/BBC-BASIC/truncate-a-file.basic
Normal file
7
Task/Truncate-a-file/BBC-BASIC/truncate-a-file.basic
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
DEF PROCtruncate(file$, size%)
|
||||
LOCAL file%
|
||||
file% = OPENUP(file$)
|
||||
IF file%=0 ERROR 100, "Could not open file"
|
||||
EXT#file% = size%
|
||||
CLOSE #file%
|
||||
ENDPROC
|
||||
23
Task/Truncate-a-file/Bracmat/truncate-a-file.bracmat
Normal file
23
Task/Truncate-a-file/Bracmat/truncate-a-file.bracmat
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
( ( 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"
|
||||
)
|
||||
);
|
||||
20
Task/Truncate-a-file/C++/truncate-a-file.cpp
Normal file
20
Task/Truncate-a-file/C++/truncate-a-file.cpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void truncateFile(string filename, int max_size) {
|
||||
std::ifstream input( filename, std::ios::binary );
|
||||
char buffer;
|
||||
string outfile = filename + ".trunc";
|
||||
ofstream appendFile(outfile, ios_base::out);
|
||||
for(int i=0; i<max_size; i++) {
|
||||
input.read( &buffer, sizeof(buffer) );
|
||||
appendFile.write(&buffer,1);
|
||||
}
|
||||
appendFile.close(); }
|
||||
|
||||
int main () {
|
||||
truncateFile("test.txt", 5);
|
||||
return 0;
|
||||
}
|
||||
28
Task/Truncate-a-file/C-sharp/truncate-a-file.cs
Normal file
28
Task/Truncate-a-file/C-sharp/truncate-a-file.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace TruncateFile
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
TruncateFile(args[0], long.Parse(args[1]));
|
||||
}
|
||||
|
||||
private static void TruncateFile(string path, long length)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
throw new ArgumentException("No file found at specified path.", "path");
|
||||
|
||||
using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Write))
|
||||
{
|
||||
if (fileStream.Length < length)
|
||||
throw new ArgumentOutOfRangeException("length",
|
||||
"The specified length is greater than that of the file.");
|
||||
|
||||
fileStream.SetLength(length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
81
Task/Truncate-a-file/C/truncate-a-file-1.c
Normal file
81
Task/Truncate-a-file/C/truncate-a-file-1.c
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
|
||||
/* Print "message: last Win32 error" to stderr. */
|
||||
void
|
||||
oops(const wchar_t *message)
|
||||
{
|
||||
wchar_t *buf;
|
||||
DWORD error;
|
||||
|
||||
buf = NULL;
|
||||
error = GetLastError();
|
||||
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
NULL, error, 0, (wchar_t *)&buf, 0, NULL);
|
||||
|
||||
if (buf) {
|
||||
fwprintf(stderr, L"%ls: %ls", message, buf);
|
||||
LocalFree(buf);
|
||||
} else {
|
||||
/* FormatMessageW failed. */
|
||||
fwprintf(stderr, L"%ls: unknown error 0x%x\n",
|
||||
message, error);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
dotruncate(wchar_t *fn, LARGE_INTEGER fp)
|
||||
{
|
||||
HANDLE fh;
|
||||
|
||||
fh = CreateFileW(fn, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
|
||||
if (fh == INVALID_HANDLE_VALUE) {
|
||||
oops(fn);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (SetFilePointerEx(fh, fp, NULL, FILE_BEGIN) == 0 ||
|
||||
SetEndOfFile(fh) == 0) {
|
||||
oops(fn);
|
||||
CloseHandle(fh);
|
||||
return 1;
|
||||
}
|
||||
|
||||
CloseHandle(fh);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Truncate or extend a file to the given length.
|
||||
*/
|
||||
int
|
||||
main()
|
||||
{
|
||||
LARGE_INTEGER fp;
|
||||
int argc;
|
||||
wchar_t **argv, *fn, junk[2];
|
||||
|
||||
/* MinGW never provides wmain(argc, argv). */
|
||||
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
|
||||
if (argv == NULL) {
|
||||
oops(L"CommandLineToArgvW");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (argc != 3) {
|
||||
fwprintf(stderr, L"usage: %ls filename length\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fn = argv[1];
|
||||
|
||||
/* fp = argv[2] converted to a LARGE_INTEGER. */
|
||||
if (swscanf(argv[2], L"%lld%1ls", &fp.QuadPart, &junk) != 1) {
|
||||
fwprintf(stderr, L"%ls: not a number\n", argv[2]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return dotruncate(fn, fp);
|
||||
}
|
||||
7
Task/Truncate-a-file/C/truncate-a-file-2.c
Normal file
7
Task/Truncate-a-file/C/truncate-a-file-2.c
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
...
|
||||
truncate(filename, length);
|
||||
ftruncate(fd, length);
|
||||
...
|
||||
5
Task/Truncate-a-file/Clojure/truncate-a-file.clj
Normal file
5
Task/Truncate-a-file/Clojure/truncate-a-file.clj
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(defn truncate [file size]
|
||||
(with-open [chan (.getChannel (java.io.FileOutputStream. file true))]
|
||||
(.truncate chan size)))
|
||||
|
||||
(truncate "truncate_test.txt" 2)
|
||||
22
Task/Truncate-a-file/D/truncate-a-file.d
Normal file
22
Task/Truncate-a-file/D/truncate-a-file.d
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import std.file, std.exception;
|
||||
|
||||
void truncateFile(in string name, in size_t newSize) {
|
||||
if (!exists(name) || !isFile(name))
|
||||
throw new Exception("File not found.");
|
||||
|
||||
auto size = getSize(name);
|
||||
if (size <= newSize)
|
||||
throw new Exception(
|
||||
"New size must be smaller than original size.");
|
||||
|
||||
auto content = cast(ubyte[])read(name, newSize);
|
||||
if (content.length != newSize)
|
||||
throw new Exception("Reading file failed.");
|
||||
|
||||
write(name, content);
|
||||
enforce(getSize(name) == newSize);
|
||||
}
|
||||
|
||||
void main() {
|
||||
truncateFile("truncate_test.txt", 0);
|
||||
}
|
||||
13
Task/Truncate-a-file/Delphi/truncate-a-file-1.delphi
Normal file
13
Task/Truncate-a-file/Delphi/truncate-a-file-1.delphi
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
procedure TruncateFile(FileName : string; NewSize : integer);
|
||||
var
|
||||
aFile: file of byte;
|
||||
begin
|
||||
Assign(aFile, FileName);
|
||||
Reset(aFile);
|
||||
try
|
||||
Seek(afile, NewSize);
|
||||
Truncate(aFile);
|
||||
finally
|
||||
Close(afile);
|
||||
end;
|
||||
end;
|
||||
11
Task/Truncate-a-file/Delphi/truncate-a-file-2.delphi
Normal file
11
Task/Truncate-a-file/Delphi/truncate-a-file-2.delphi
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
procedure TruncateFile(FileName : string; NewSize : integer);
|
||||
var
|
||||
Stream : TFileStream;
|
||||
begin
|
||||
Stream := TFileStream.Create(FileName, fmOpenReadWrite, fmShareExclusive);
|
||||
try
|
||||
Stream.Size := NewSize;
|
||||
finally
|
||||
Stream.Free;
|
||||
end;
|
||||
end;
|
||||
28
Task/Truncate-a-file/Elena/truncate-a-file.elena
Normal file
28
Task/Truncate-a-file/Elena/truncate-a-file.elena
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import system'io;
|
||||
import extensions;
|
||||
|
||||
extension fileOp : File
|
||||
{
|
||||
set Length(int len)
|
||||
{
|
||||
auto stream := FileStream.openForEdit(self);
|
||||
|
||||
stream.Length := len;
|
||||
|
||||
stream.close()
|
||||
}
|
||||
}
|
||||
|
||||
public program()
|
||||
{
|
||||
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]);
|
||||
var length := program_arguments[2].toInt();
|
||||
|
||||
ifnot(file.Available)
|
||||
{ console.printLine("File ",file," does not exist"); AbortException.raise() };
|
||||
|
||||
file.Length := length
|
||||
}
|
||||
11
Task/Truncate-a-file/Erlang/truncate-a-file.erl
Normal file
11
Task/Truncate-a-file/Erlang/truncate-a-file.erl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
-module( truncate ).
|
||||
|
||||
-export( [file/2] ).
|
||||
|
||||
file( Name, Size ) ->
|
||||
{file_exists, true} = {file_exists, filelib:is_file( Name )},
|
||||
{ok, IO} = file:open( Name, [read, write] ),
|
||||
{ok, Max} = file:position( IO, eof ),
|
||||
{correct_size, true} = {correct_size, (Size < Max)},
|
||||
{ok, Size} = file:position( IO, {bof, Size} ),
|
||||
ok = file:truncate( IO ).
|
||||
13
Task/Truncate-a-file/F-Sharp/truncate-a-file.fs
Normal file
13
Task/Truncate-a-file/F-Sharp/truncate-a-file.fs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
open System
|
||||
open System.IO
|
||||
|
||||
let truncateFile path length =
|
||||
if not (File.Exists(path)) then failwith ("File not found: " + path)
|
||||
use fileStream = new FileStream(path, FileMode.Open, FileAccess.Write)
|
||||
if (fileStream.Length < length) then failwith "The specified length is greater than the current file length."
|
||||
fileStream.SetLength(length)
|
||||
|
||||
[<EntryPoint>]
|
||||
let main args =
|
||||
truncateFile args.[0] (Int64.Parse(args.[1]))
|
||||
0
|
||||
46
Task/Truncate-a-file/Fortran/truncate-a-file.f
Normal file
46
Task/Truncate-a-file/Fortran/truncate-a-file.f
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
SUBROUTINE CROAK(GASP) !Something bad has happened.
|
||||
CHARACTER*(*) GASP !As noted.
|
||||
WRITE (6,*) "Oh dear. ",GASP !So, gasp away.
|
||||
STOP "++ungood." !Farewell, cruel world.
|
||||
END !No return from this.
|
||||
|
||||
SUBROUTINE FILEHACK(FNAME,NB)
|
||||
CHARACTER*(*) FNAME !Name for the file.
|
||||
INTEGER NB !Number of bytes to survive.
|
||||
INTEGER L !A counter for te length of the file.
|
||||
INTEGER F,T !Mnemonics for file unit numbers.
|
||||
PARAMETER (F=66,T=67) !These should do.
|
||||
LOGICAL EXIST !Same as the mnemonic so left/right can be forgotten.
|
||||
CHARACTER*1 B !The worker!
|
||||
IF (FNAME.EQ."") CALL CROAK("Blank file name!")
|
||||
IF (NB.LE.0) CALL CROAK("Chop must be positive!")
|
||||
INQUIRE(FILE = FNAME, EXIST = EXIST) !This mishap is frequent, so attend to it.
|
||||
IF (.NOT.EXIST) CALL CROAK("Can't find a file called "//FNAME) !Tough love.
|
||||
OPEN (F,FILE=FNAME,STATUS="OLD",ACTION="READWRITE", !Grab the source file.
|
||||
1 FORM="UNFORMATTED",RECL=1,ACCESS="DIRECT") !Oh dear.
|
||||
OPEN (T,STATUS="SCRATCH",FORM="UNFORMATTED",RECL=1) !Request a temporary file.
|
||||
|
||||
Copy the desired "records" to the temporary file.
|
||||
10 DO L = 1,NB !Only up to a point.
|
||||
READ (F,REC = L,ERR = 20) B !One whole byte!
|
||||
WRITE (T) B !And, write it too!
|
||||
END DO !Again.
|
||||
20 IF (L.LE.NB) CALL CROAK("Short file!") !Should end the loop with L = NB + 1.
|
||||
Convert from input to output...
|
||||
REWIND T !Not CLOSE! That would discard the file!
|
||||
CLOSE(F) !The source file still exists.
|
||||
OPEN (F,FILE=FNAME,FORM="FORMATTED", !But,
|
||||
1 ACTION="WRITE",STATUS="REPLACE") !This dooms it!
|
||||
Copy from the temporary file.
|
||||
DO L = 1,NB !A certain number only.
|
||||
READ (T) B !One at at timne.
|
||||
WRITE (F,"(A1,$)") B !The $, obviously, means no end-of-record appendage.
|
||||
END DO !And again.
|
||||
Completed.
|
||||
30 CLOSE(T) !Abandon the temporary file.
|
||||
CLOSE(F) !Finished with the source file.
|
||||
END !Done.
|
||||
|
||||
PROGRAM CHOPPER
|
||||
CALL FILEHACK("foobar.txt",12)
|
||||
END
|
||||
20
Task/Truncate-a-file/FreeBASIC/truncate-a-file.basic
Normal file
20
Task/Truncate-a-file/FreeBASIC/truncate-a-file.basic
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
Sub truncateFile (archivo As String, longitud As Long)
|
||||
If Len(Dir(archivo)) Then
|
||||
Dim f As Long, c As String
|
||||
f = Freefile
|
||||
Open archivo For Binary As #f
|
||||
If longitud > Lof(f) Then
|
||||
Close #f
|
||||
Error 62 'Input past end of file
|
||||
Else
|
||||
c = Space(longitud)
|
||||
Get #f, 1, c
|
||||
Close f
|
||||
Open archivo For Output As #f
|
||||
Print #f, c;
|
||||
Close #f
|
||||
End If
|
||||
Else
|
||||
Error 53 'File not found
|
||||
End If
|
||||
End Sub
|
||||
8
Task/Truncate-a-file/Go/truncate-a-file.go
Normal file
8
Task/Truncate-a-file/Go/truncate-a-file.go
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
if err := os.Truncate("filename", newSize); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
5
Task/Truncate-a-file/Haskell/truncate-a-file.hs
Normal file
5
Task/Truncate-a-file/Haskell/truncate-a-file.hs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
setFileSize :: FilePath -> FileOffset -> IO ()
|
||||
-- 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.
|
||||
1
Task/Truncate-a-file/Icon/truncate-a-file.icon
Normal file
1
Task/Truncate-a-file/Icon/truncate-a-file.icon
Normal file
|
|
@ -0,0 +1 @@
|
|||
truncate(f := open(filename, "bu"), newsizeinbytes) & close(f)
|
||||
2
Task/Truncate-a-file/J/truncate-a-file-1.j
Normal file
2
Task/Truncate-a-file/J/truncate-a-file-1.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
require 'files' NB. needed for versions prior to J7
|
||||
ftruncate=: ] fwrite~ ] fread@; 0 , [
|
||||
5
Task/Truncate-a-file/J/truncate-a-file-2.j
Normal file
5
Task/Truncate-a-file/J/truncate-a-file-2.j
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(1000$ 'abcdefg') fwrite 'text.txt' NB. create test file
|
||||
567 ftruncate 'test.txt' NB. truncate test file
|
||||
567
|
||||
fsize 'test.txt' NB. check new file size
|
||||
567
|
||||
17
Task/Truncate-a-file/Java/truncate-a-file.java
Normal file
17
Task/Truncate-a-file/Java/truncate-a-file.java
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
public class TruncFile {
|
||||
public static void main(String[] args) throws IOException{
|
||||
if(args.length < 2){
|
||||
System.out.println("Usage: java TruncFile fileName newSize");
|
||||
return;
|
||||
}
|
||||
//turn on "append" so it doesn't clear the file
|
||||
FileChannel outChan = new FileOutputStream(args[0], true).getChannel();
|
||||
long newSize = Long.parseLong(args[1]);
|
||||
outChan.truncate(newSize);
|
||||
outChan.close();
|
||||
}
|
||||
}
|
||||
5
Task/Truncate-a-file/Julia/truncate-a-file.julia
Normal file
5
Task/Truncate-a-file/Julia/truncate-a-file.julia
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
function truncate_file(fname, size):
|
||||
open(fname, "r+") do f
|
||||
truncate(f, size)
|
||||
end
|
||||
end
|
||||
25
Task/Truncate-a-file/Kotlin/truncate-a-file.kotlin
Normal file
25
Task/Truncate-a-file/Kotlin/truncate-a-file.kotlin
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// version 1.1.2
|
||||
|
||||
import java.io.FileOutputStream
|
||||
import java.nio.channels.FileChannel
|
||||
|
||||
fun truncateFile(fileName: String, newSize: Long) {
|
||||
var fc: FileChannel? = null
|
||||
try {
|
||||
fc = FileOutputStream(fileName, true).channel
|
||||
if (newSize >= fc.size())
|
||||
println("Requested file size isn't less than existing size")
|
||||
else
|
||||
fc.truncate(newSize)
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
println(ex.message)
|
||||
}
|
||||
finally {
|
||||
fc!!.close()
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
truncateFile("test.txt", 10)
|
||||
}
|
||||
19
Task/Truncate-a-file/Lasso/truncate-a-file.lasso
Normal file
19
Task/Truncate-a-file/Lasso/truncate-a-file.lasso
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
define file_truncate(path::string, size::integer) => {
|
||||
|
||||
local(file = file(#path))
|
||||
|
||||
fail_if(not(#file -> exists), -1, 'There is no file at the given path')
|
||||
fail_if(#file -> size < #size, -1, 'No point in truncating a file to a larger size than it already is')
|
||||
|
||||
#file -> setSize(#size)
|
||||
|
||||
}
|
||||
local(filepath = '//Library/WebServer/Documents/Lasso9cli/trunk/testing/lorem_ipsum_long.txt')
|
||||
|
||||
stdoutnl(file(#filepath) -> readbytes)
|
||||
stdoutnl('Original size: ' + file(#filepath) -> size)
|
||||
|
||||
file_truncate(#filepath, 300)
|
||||
|
||||
stdoutnl(file(#filepath) -> readbytes)
|
||||
stdout(file('Truncated size: ' + #filepath) -> size)
|
||||
30
Task/Truncate-a-file/Liberty-BASIC/truncate-a-file.basic
Normal file
30
Task/Truncate-a-file/Liberty-BASIC/truncate-a-file.basic
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
dim info$( 50, 50) ' NB pre-dimension before calling file-exists
|
||||
' needed for file-exists function
|
||||
open "test.dat" for output as #1 'create file
|
||||
for i = 1 to 10000
|
||||
#1 chr$( int( 256 *rnd( 1)));
|
||||
next
|
||||
close #1
|
||||
|
||||
call truncateFile, "test.dat", 5000
|
||||
|
||||
wait
|
||||
|
||||
sub truncateFile fn$, length
|
||||
if fileExists( DefaultDir$, fn$) =0 then notice "No such file": exit sub
|
||||
open fn$ for input as #i
|
||||
file$ =input$( #i, lof( #i))
|
||||
if len( file$) <length then notice "Too short": close #i: exit sub
|
||||
file$ =left$( file$, length)
|
||||
close #i
|
||||
open "test.dat" for output as #o
|
||||
#o file$
|
||||
close #o
|
||||
end sub
|
||||
|
||||
function fileExists( path$, filename$)
|
||||
files path$, filename$, info$()
|
||||
fileExists =val( info$( 0, 0)) 'non zero is true
|
||||
end function
|
||||
|
||||
end
|
||||
28
Task/Truncate-a-file/Lingo/truncate-a-file-1.lingo
Normal file
28
Task/Truncate-a-file/Lingo/truncate-a-file-1.lingo
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
----------------------------------------
|
||||
-- Truncates file
|
||||
-- @param {string} filename
|
||||
-- @param {integer} length
|
||||
-- @return {bool} success
|
||||
----------------------------------------
|
||||
on truncate (filename, length)
|
||||
fp = xtra("fileIO").new()
|
||||
fp.openFile(filename, 0)
|
||||
if fp.status() then return false
|
||||
if fp.getLength()=length then
|
||||
-- nothing to do
|
||||
fp.closeFile()
|
||||
return true
|
||||
end if
|
||||
data = fp.readByteArray(length)
|
||||
if data.length<>length then
|
||||
fp.closeFile()
|
||||
return false
|
||||
end if
|
||||
fp.delete()
|
||||
fp.createFile(filename)
|
||||
fp.openFile(filename, 2)
|
||||
fp.writeByteArray(data)
|
||||
ok = fp.status()=0
|
||||
fp.closeFile()
|
||||
return ok
|
||||
end
|
||||
2
Task/Truncate-a-file/Lingo/truncate-a-file-2.lingo
Normal file
2
Task/Truncate-a-file/Lingo/truncate-a-file-2.lingo
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
-- truncates file to 10 KB length
|
||||
bx_file_truncate(_movie.path&"foo.dat", 10240)
|
||||
14
Task/Truncate-a-file/Lua/truncate-a-file.lua
Normal file
14
Task/Truncate-a-file/Lua/truncate-a-file.lua
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
function truncate (filename, length)
|
||||
local inFile = io.open(filename, 'r')
|
||||
if not inFile then
|
||||
error("Specified filename does not exist")
|
||||
end
|
||||
local wholeFile = inFile:read("*all")
|
||||
inFile:close()
|
||||
if length >= wholeFile:len() then
|
||||
error("Provided length is not less than current file length")
|
||||
end
|
||||
local outFile = io.open(filename, 'w')
|
||||
outFile:write(wholeFile:sub(1, length))
|
||||
outFile:close()
|
||||
end
|
||||
9
Task/Truncate-a-file/MATLAB/truncate-a-file.m
Normal file
9
Task/Truncate-a-file/MATLAB/truncate-a-file.m
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function truncate_a_file(fn,count);
|
||||
|
||||
fid=fopen(fn,'r');
|
||||
s = fread(fid,count,'uint8');
|
||||
fclose(fid);
|
||||
|
||||
fid=fopen(fn,'w');
|
||||
s = fwrite(fid,s,'uint8');
|
||||
fclose(fid);
|
||||
5
Task/Truncate-a-file/Mathematica/truncate-a-file.math
Normal file
5
Task/Truncate-a-file/Mathematica/truncate-a-file.math
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Truncate[file_, n_] := Module[{filename = file, nbbytes = n, temp},
|
||||
temp = $TemporaryPrefix <> filename;
|
||||
BinaryWrite[temp, BinaryReadList[filename, "Byte", nbbytes]];
|
||||
Close[temp]; DeleteFile[filename]; RenameFile[temp, filename];
|
||||
]
|
||||
3
Task/Truncate-a-file/Nim/truncate-a-file.nim
Normal file
3
Task/Truncate-a-file/Nim/truncate-a-file.nim
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
import posix
|
||||
|
||||
discard truncate("filename", 1024)
|
||||
2
Task/Truncate-a-file/OCaml/truncate-a-file-1.ocaml
Normal file
2
Task/Truncate-a-file/OCaml/truncate-a-file-1.ocaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
val truncate : string -> int -> unit
|
||||
(** Truncates the named file to the given size. *)
|
||||
2
Task/Truncate-a-file/OCaml/truncate-a-file-2.ocaml
Normal file
2
Task/Truncate-a-file/OCaml/truncate-a-file-2.ocaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
val ftruncate : file_descr -> int -> unit
|
||||
(** Truncates the file corresponding to the given descriptor to the given size. *)
|
||||
3
Task/Truncate-a-file/PARI-GP/truncate-a-file.parigp
Normal file
3
Task/Truncate-a-file/PARI-GP/truncate-a-file.parigp
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
install("truncate", "isL", "trunc")
|
||||
|
||||
trunc("/tmp/test.file", 20)
|
||||
28
Task/Truncate-a-file/PL-I/truncate-a-file.pli
Normal file
28
Task/Truncate-a-file/PL-I/truncate-a-file.pli
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* Parameters to be read in by the program are: */
|
||||
/* 1. the name of the file to be truncated, and */
|
||||
/* 2. the size of file after truncation. */
|
||||
|
||||
truncate_file: procedure options (main); /* 12 July 2012 */
|
||||
declare (i, n) fixed binary (31);
|
||||
declare filename character(50) varying;
|
||||
declare in file record input, out file record output;
|
||||
|
||||
put ('What is the name of the file to be truncated?');
|
||||
get edit (filename) (L);
|
||||
put ('What is the length of file to be retained?');
|
||||
get (n);
|
||||
|
||||
begin;
|
||||
declare c(n) character (1), ch character (1);
|
||||
|
||||
open file (in) title ('/' || filename || ',type(fixed),recsize(1)' )
|
||||
input;
|
||||
do i = 1 to n; read file (in) into (ch); c(i) = ch; end;
|
||||
close file (in);
|
||||
|
||||
open file (out) title ('/' || filename || ',append(n),type(fixed),
|
||||
recsize(' || trim(n) || ')' );
|
||||
write file (out) from (c);
|
||||
close file (out);
|
||||
end;
|
||||
end truncate_file;
|
||||
36
Task/Truncate-a-file/Pascal/truncate-a-file.pas
Normal file
36
Task/Truncate-a-file/Pascal/truncate-a-file.pas
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
Program FileTruncate;
|
||||
|
||||
uses
|
||||
SysUtils;
|
||||
|
||||
var
|
||||
myfile: file of byte;
|
||||
filename: string;
|
||||
position: integer;
|
||||
|
||||
begin
|
||||
write('File for truncation: ');
|
||||
readln(filename);
|
||||
if not FileExists(filename) then
|
||||
begin
|
||||
writeln('Error: File does not exist.');
|
||||
exit;
|
||||
end;
|
||||
|
||||
write('Truncate position: ');
|
||||
readln(position);
|
||||
|
||||
Assign(myfile, filename);
|
||||
Reset(myfile);
|
||||
if FileSize(myfile) < position then
|
||||
begin
|
||||
writeln('Warning: The file "', filename, '" is too short. No need to truncate at position ', position);
|
||||
Close(myfile);
|
||||
exit;
|
||||
end;
|
||||
|
||||
Seek(myfile, position);
|
||||
Truncate(myfile);
|
||||
Close(myfile);
|
||||
writeln('File "', filename, '" truncated at position ', position, '.');
|
||||
end.
|
||||
7
Task/Truncate-a-file/Perl/truncate-a-file.pl
Normal file
7
Task/Truncate-a-file/Perl/truncate-a-file.pl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# Open a file for writing, and truncate it to 1234 bytes.
|
||||
open FOO, ">>file" or die;
|
||||
truncate(FOO, 1234);
|
||||
close FOO;
|
||||
|
||||
# Truncate a file to 567 bytes.
|
||||
truncate("file", 567);
|
||||
8
Task/Truncate-a-file/Phix/truncate-a-file.phix
Normal file
8
Task/Truncate-a-file/Phix/truncate-a-file.phix
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(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>
|
||||
<!--
|
||||
2
Task/Truncate-a-file/PicoLisp/truncate-a-file-1.l
Normal file
2
Task/Truncate-a-file/PicoLisp/truncate-a-file-1.l
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(de truncate (File Len)
|
||||
(native "@" "truncate" 'I File Len) )
|
||||
2
Task/Truncate-a-file/PicoLisp/truncate-a-file-2.l
Normal file
2
Task/Truncate-a-file/PicoLisp/truncate-a-file-2.l
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(de truncate (File Len)
|
||||
(call "truncate" "-s" Len File) )
|
||||
17
Task/Truncate-a-file/PowerBASIC/truncate-a-file.basic
Normal file
17
Task/Truncate-a-file/PowerBASIC/truncate-a-file.basic
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
SUB truncateFile (file AS STRING, length AS DWORD)
|
||||
IF LEN(DIR$(file)) THEN
|
||||
DIM f AS LONG
|
||||
f = FREEFILE
|
||||
OPEN file FOR BINARY AS f
|
||||
IF length > LOF(f) THEN
|
||||
CLOSE f
|
||||
ERROR 62 'Input past end
|
||||
ELSE
|
||||
SEEK f, length + 1
|
||||
SETEOF f
|
||||
CLOSE f
|
||||
END IF
|
||||
ELSE
|
||||
ERROR 53 'File not found
|
||||
END IF
|
||||
END SUB
|
||||
3
Task/Truncate-a-file/PowerShell/truncate-a-file.psh
Normal file
3
Task/Truncate-a-file/PowerShell/truncate-a-file.psh
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Function Truncate-File(fname) {
|
||||
$null | Set-Content -Path "$fname"
|
||||
}
|
||||
13
Task/Truncate-a-file/PureBasic/truncate-a-file.basic
Normal file
13
Task/Truncate-a-file/PureBasic/truncate-a-file.basic
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
Procedure SetFileSize(File$, length.q)
|
||||
Protected fh, pos, i
|
||||
If FileSize(File$) < length
|
||||
Debug "File to small, is a directory or does not exist."
|
||||
ProcedureReturn #False
|
||||
Else
|
||||
fh = OpenFile(#PB_Any, File$)
|
||||
FileSeek(fh, length)
|
||||
TruncateFile(fh)
|
||||
CloseFile(fh)
|
||||
EndIf
|
||||
ProcedureReturn #True
|
||||
EndProcedure
|
||||
8
Task/Truncate-a-file/Python/truncate-a-file.py
Normal file
8
Task/Truncate-a-file/Python/truncate-a-file.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
def truncate_file(name, length):
|
||||
if not os.path.isfile(name):
|
||||
return False
|
||||
if length >= os.path.getsize(name):
|
||||
return False
|
||||
with open(name, 'ab') as f:
|
||||
f.truncate(length)
|
||||
return True
|
||||
28
Task/Truncate-a-file/R/truncate-a-file.r
Normal file
28
Task/Truncate-a-file/R/truncate-a-file.r
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
truncate_file <- function(filename, n_bytes) {
|
||||
|
||||
# check file exists and size is greater than n_bytes
|
||||
stopifnot(
|
||||
"file does not exist"= file.exists(filename),
|
||||
"not enough bytes in file"= file.size(filename) >= n_bytes
|
||||
)
|
||||
|
||||
# read bytes from input file
|
||||
input.con <- file(filename, "rb")
|
||||
bindata <- readBin(input.con, integer(), n=n_bytes/4)
|
||||
close(input.con)
|
||||
|
||||
# write bytes to temporary file
|
||||
tmp.filename <- tempfile()
|
||||
output.con <- file(tmp.filename, "wb")
|
||||
writeBin(bindata, output.con)
|
||||
close(output.con)
|
||||
|
||||
# double check that everything worked before overwriting original file
|
||||
stopifnot(
|
||||
"temp file is not expected size"= file.size(tmp.filename) == n_bytes
|
||||
)
|
||||
|
||||
# overwrite input file with temporary file
|
||||
file.rename(tmp.filename, filename)
|
||||
|
||||
}
|
||||
20
Task/Truncate-a-file/REXX/truncate-a-file-1.rexx
Normal file
20
Task/Truncate-a-file/REXX/truncate-a-file-1.rexx
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/*REXX program truncates a file to a specified (and smaller) number of bytes. */
|
||||
parse arg siz FID /*obtain required arguments from the CL*/
|
||||
FID=strip(FID) /*elide FID leading/trailing blanks. */
|
||||
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
|
||||
_=charin(FID,1,siz+1) /*position file and read a wee bit more*/
|
||||
#=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 information to terminal.*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
ser: say '***error***' arg(1); exit 13 /*display an error message and exit. */
|
||||
27
Task/Truncate-a-file/REXX/truncate-a-file-2.rexx
Normal file
27
Task/Truncate-a-file/REXX/truncate-a-file-2.rexx
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/*REXX program truncates a file to a specified (and smaller) number of bytes. */
|
||||
parse arg siz FID /*obtain required arguments from the CL*/
|
||||
FID=strip(FID) /*elide FID leading/trailing blanks. */
|
||||
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 /*Nothing read? Then we're done reading*/
|
||||
_=_ || 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 information to terminal.*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
ser: say '***error***' arg(1); exit 13 /*display an error message and exit. */
|
||||
7
Task/Truncate-a-file/Racket/truncate-a-file.rkt
Normal file
7
Task/Truncate-a-file/Racket/truncate-a-file.rkt
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#lang racket
|
||||
|
||||
(define (truncate file size)
|
||||
(unless (file-exists? file) (error 'truncat "missing file: ~a" file))
|
||||
(when (> size (file-size file)) (printf "Warning: extending file size.\n"))
|
||||
(call-with-output-file* file #:exists 'update
|
||||
(λ(o) (file-truncate o size))))
|
||||
12
Task/Truncate-a-file/Raku/truncate-a-file-1.raku
Normal file
12
Task/Truncate-a-file/Raku/truncate-a-file-1.raku
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
use NativeCall;
|
||||
|
||||
sub truncate(Str, int32 --> int32) is native {*}
|
||||
|
||||
sub MAIN (Str $file, Int $to) {
|
||||
given $file.IO {
|
||||
.e or die "$file doesn't exist";
|
||||
.w or die "$file isn't writable";
|
||||
.s >= $to or die "$file is not big enough to truncate";
|
||||
}
|
||||
truncate($file, $to) == 0 or die "Truncation was unsuccessful";
|
||||
}
|
||||
1
Task/Truncate-a-file/Raku/truncate-a-file-2.raku
Normal file
1
Task/Truncate-a-file/Raku/truncate-a-file-2.raku
Normal file
|
|
@ -0,0 +1 @@
|
|||
spurt $file, slurp($file).substr($to);
|
||||
5
Task/Truncate-a-file/Ring/truncate-a-file.ring
Normal file
5
Task/Truncate-a-file/Ring/truncate-a-file.ring
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
file = "C:\Ring\ReadMe.txt"
|
||||
fp = read(file)
|
||||
fpstr = left(fp, 100)
|
||||
see fpstr + nl
|
||||
write(file, fpstr)
|
||||
8
Task/Truncate-a-file/Ruby/truncate-a-file.rb
Normal file
8
Task/Truncate-a-file/Ruby/truncate-a-file.rb
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# Open a file for writing, and truncate it to 1234 bytes.
|
||||
File.open("file", "ab") do |f|
|
||||
f.truncate(1234)
|
||||
f << "Killroy was here" # write to file
|
||||
end # file is closed now.
|
||||
|
||||
# Just truncate a file to 567 bytes.
|
||||
File.truncate("file", 567)
|
||||
24
Task/Truncate-a-file/Rust/truncate-a-file.rust
Normal file
24
Task/Truncate-a-file/Rust/truncate-a-file.rust
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use std::path::Path;
|
||||
use std::fs;
|
||||
|
||||
fn truncate_file<P: AsRef<Path>>(filename: P, filesize: usize) -> Result<(), Error> {
|
||||
use Error::*;
|
||||
let file = fs::read(&filename).or(Err(NotFound))?;
|
||||
|
||||
if filesize > file.len() {
|
||||
return Err(FilesizeTooSmall)
|
||||
}
|
||||
|
||||
fs::write(&filename, &file[..filesize]).or(Err(UnableToWrite))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Error {
|
||||
/// File not found
|
||||
NotFound,
|
||||
/// Truncated size would be larger than the current size
|
||||
FilesizeTooSmall,
|
||||
/// Likely due to having read but not write permissions
|
||||
UnableToWrite,
|
||||
}
|
||||
10
Task/Truncate-a-file/Scala/truncate-a-file.scala
Normal file
10
Task/Truncate-a-file/Scala/truncate-a-file.scala
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import java.io.FileOutputStream
|
||||
|
||||
object TruncFile extends App {
|
||||
if (args.length < 2) println("Usage: java TruncFile fileName newSize")
|
||||
else { //turn on "append" so it doesn't clear the file
|
||||
val outChan = new FileOutputStream(args(0), true).getChannel()
|
||||
val newSize = args(1).toLong
|
||||
outChan.truncate(newSize)
|
||||
}
|
||||
}
|
||||
9
Task/Truncate-a-file/Sidef/truncate-a-file.sidef
Normal file
9
Task/Truncate-a-file/Sidef/truncate-a-file.sidef
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
func truncate(filename, len) {
|
||||
var file = File(filename);
|
||||
len > file.size ->
|
||||
&& die "The provided length is greater than the length of the file";
|
||||
file.truncate(len);
|
||||
}
|
||||
|
||||
# truncate "file.ext" to 1234 bytes
|
||||
truncate("file.ext", 1234);
|
||||
11
Task/Truncate-a-file/Standard-ML/truncate-a-file.ml
Normal file
11
Task/Truncate-a-file/Standard-ML/truncate-a-file.ml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
local
|
||||
open Posix.FileSys
|
||||
val perm = S.flags [S.irusr, S.iwusr, S.irgrp, S.iwgrp, S.iroth, S.iwoth]
|
||||
in
|
||||
fun truncate (path, len) =
|
||||
let
|
||||
val fd = createf (path, O_WRONLY, O.noctty, perm)
|
||||
in
|
||||
ftruncate (fd, len); Posix.IO.close fd
|
||||
end
|
||||
end
|
||||
5
Task/Truncate-a-file/Tcl/truncate-a-file.tcl
Normal file
5
Task/Truncate-a-file/Tcl/truncate-a-file.tcl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package require Tcl 8.5
|
||||
|
||||
set f [open "file" r+]; # Truncation is done on channels
|
||||
chan truncate $f 1234; # Truncate at a particular length (in bytes)
|
||||
close $f
|
||||
3
Task/Truncate-a-file/UNIX-Shell/truncate-a-file-1.sh
Normal file
3
Task/Truncate-a-file/UNIX-Shell/truncate-a-file-1.sh
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# Truncate a file named "myfile" to 1440 kilobytes.
|
||||
ls myfile >/dev/null &&
|
||||
dd if=/dev/null of=myfile bs=1 seek=1440k
|
||||
2
Task/Truncate-a-file/UNIX-Shell/truncate-a-file-2.sh
Normal file
2
Task/Truncate-a-file/UNIX-Shell/truncate-a-file-2.sh
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# Truncate a file named "myfile" to 1440 kilobytes.
|
||||
truncate -s 1440k myfile
|
||||
87
Task/Truncate-a-file/UNIX-Shell/truncate-a-file-3.sh
Normal file
87
Task/Truncate-a-file/UNIX-Shell/truncate-a-file-3.sh
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
# 1. simplest one-liner
|
||||
-bash$ >file
|
||||
|
||||
# or the more clear/verbose:
|
||||
echo -n>file
|
||||
|
||||
# 2. checks for file as input prior to truncate:
|
||||
f="file";echo -n<$f>$f
|
||||
|
||||
# double-check file size
|
||||
ls -1 file # it's trunc
|
||||
|
||||
# 3. multiple files (glob)
|
||||
glob=$(ls -1 file[0-9]); for f in $glob; do echo -n<$f>$f; done
|
||||
|
||||
|
||||
## OUTPUT:
|
||||
# 1. one-liner
|
||||
-bash-4.2$ echo -n>bad
|
||||
+ echo -n
|
||||
-bash-4.2$ ls -l bad; cat bad
|
||||
+ ls -l bad
|
||||
-rw-r--r-- 1 fieldsa fieldsa 0 Dec 4 20:07 bad
|
||||
+ cat bad
|
||||
-bash-4.2$
|
||||
|
||||
# 2. checks
|
||||
-bash-4.2$ set -x; f="bad"; echo -n<$f>$f
|
||||
+ f=bad
|
||||
+ echo -n
|
||||
-bash: bad: No such file or directory
|
||||
|
||||
# Try again with the shortest version: f="bad"<$f>$f
|
||||
+ ls -l bad
|
||||
-rw-r--r-- 1 fieldsa fieldsa 8192 Dec 5 01:04 bad
|
||||
-bash-4.2$ f="bad" <$f>$f
|
||||
+ f=bad
|
||||
-bash-4.2$ ls -l
|
||||
+ ls -l
|
||||
-rw-r--r-- 1 fieldsa fieldsa 0 Dec 5 01:04 bad
|
||||
-bash-4.2$ rm bad
|
||||
+ rm bad
|
||||
-bash-4.2$ f="bad" <$f>$f
|
||||
+ f=bad
|
||||
-bash: bad: No such file or directory
|
||||
|
||||
# Now a bad file exists
|
||||
-bash-4.2$ echo 'abc'>bad
|
||||
+ echo abc
|
||||
-bash-4.2$ ls -l bad;cat<bad
|
||||
+ ls -l bad
|
||||
-rw-r--r-- 1 fieldsa fieldsa 4 Dec 4 19:21 bad
|
||||
+ cat
|
||||
abc
|
||||
|
||||
-bash-4.2$ f="bad"; echo -n<$f>$f
|
||||
+ f=bad
|
||||
+ echo -n
|
||||
-bash-4.2$ ls -l bad;cat<bad;echo "_EOF_"
|
||||
+ ls -l bad
|
||||
-rw-r--r-- 1 fieldsa fieldsa 0 Dec 4 19:23 bad
|
||||
+ cat
|
||||
+ echo _EOF_
|
||||
_EOF_
|
||||
|
||||
-bash-4.2$ rm bad
|
||||
+ rm bad
|
||||
-bash-4.2$ f="bad" <$f&&echo -n>$f
|
||||
+ f=bad
|
||||
-bash: bad: No such file or directory
|
||||
|
||||
# 4. trunc(): trunc file size # truncate at length
|
||||
-bash-4.2$ trunc() { IFS= read -N $2 b<"$1"; echo -n "$b">"$1"; }
|
||||
-bash-4.2$ echo "abcdef">bad; ls -l bad; cat bad; trunc bad 3; ls -l bad; cat bad; echo "_EOF_"
|
||||
+ echo abcdef
|
||||
+ ls -l bad
|
||||
-rw-r--r-- 1 fieldsa fieldsa 7 Dec 4 19:59 bad
|
||||
+ cat bad
|
||||
abcdef
|
||||
+ trunc bad 3
|
||||
+ read -N 3 b
|
||||
+ echo -n abc
|
||||
+ ls -l bad
|
||||
-rw-r--r-- 1 fieldsa fieldsa 3 Dec 4 19:59 bad
|
||||
+ cat bad
|
||||
abc+ echo _EOF_
|
||||
_EOF_
|
||||
39
Task/Truncate-a-file/VBScript/truncate-a-file.vb
Normal file
39
Task/Truncate-a-file/VBScript/truncate-a-file.vb
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
Sub truncate(fpath,n)
|
||||
'Check if file exist
|
||||
Set objfso = CreateObject("Scripting.FileSystemObject")
|
||||
If objfso.FileExists(fpath) = False Then
|
||||
WScript.Echo fpath & " does not exist"
|
||||
Exit Sub
|
||||
End If
|
||||
content = ""
|
||||
'stream the input file
|
||||
Set objinstream = CreateObject("Adodb.Stream")
|
||||
With objinstream
|
||||
.Type = 1
|
||||
.Open
|
||||
.LoadFromFile(fpath)
|
||||
'check if the specified size is larger than the file content
|
||||
If n <= .Size Then
|
||||
content = .Read(n)
|
||||
Else
|
||||
WScript.Echo "The specified size is larger than the file content"
|
||||
Exit Sub
|
||||
End If
|
||||
.Close
|
||||
End With
|
||||
'write the truncated version
|
||||
Set objoutstream = CreateObject("Adodb.Stream")
|
||||
With objoutstream
|
||||
.Type = 1
|
||||
.Open
|
||||
.Write content
|
||||
.SaveToFile fpath,2
|
||||
.Close
|
||||
End With
|
||||
Set objinstream = Nothing
|
||||
Set objoutstream = Nothing
|
||||
Set objfso = Nothing
|
||||
End Sub
|
||||
|
||||
'testing
|
||||
Call truncate("C:\temp\test.txt",30)
|
||||
15
Task/Truncate-a-file/Wren/truncate-a-file.wren
Normal file
15
Task/Truncate-a-file/Wren/truncate-a-file.wren
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import "/ioutil" for FileUtil
|
||||
|
||||
var fileName = "temp.txt"
|
||||
|
||||
// create a file of length 26 bytes
|
||||
FileUtil.write(fileName, "abcdefghijklmnopqrstuvwxyz")
|
||||
System.print("Contents before truncation: %(FileUtil.read(fileName))")
|
||||
|
||||
// truncate file to 13 bytes
|
||||
FileUtil.truncate(fileName, 13)
|
||||
System.print("Contents after truncation : %(FileUtil.read(fileName))")
|
||||
|
||||
// attempt to truncate file to 20 bytes
|
||||
FileUtil.truncate(fileName, 20)
|
||||
System.print("Contents are still : %(FileUtil.read(fileName))")
|
||||
32
Task/Truncate-a-file/XPL0/truncate-a-file.xpl0
Normal file
32
Task/Truncate-a-file/XPL0/truncate-a-file.xpl0
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
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);
|
||||
]
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
10 CLEAR 29999
|
||||
20 INPUT "Which file do you want to truncate?";f$
|
||||
30 PRINT "Start tape to load file to truncate."
|
||||
40 LOAD f$ CODE 30000
|
||||
50 "Input how many bytes do you want to keep?";n
|
||||
60 PRINT "Please rewind the tape and press record."
|
||||
70 SAVE f$ CODE 30000,n
|
||||
80 STOP
|
||||
Loading…
Add table
Add a link
Reference in a new issue