Truncate a file: Difference between revisions

ReSort alphabetically, {{header|Pascal}}: add example
(Haskell information)
(ReSort alphabetically, {{header|Pascal}}: add example)
Line 120:
</lang>
 
=={{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.
<lang Unicon>truncate(f := open(filename, "bu"), newsizeinbytes) & close(f)</lang>
Line 144:
}
}</lang>
 
=={{header|OCaml}}==
 
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>:
 
<lang ocaml>val truncate : string -> int -> unit
(** Truncates the named file to the given size. *)</lang>
 
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:
 
<lang ocaml>val ftruncate : file_descr -> int -> unit
(** Truncates the file corresponding to the given descriptor to the given size. *)</lang>
 
=={{header|Liberty BASIC}}==
Line 194 ⟶ 182:
</lang>
=={{header|OCaml}}==
 
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>:
=={{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.
<lang ocaml>val truncate : string -> int -> unit
<lang PureBasic>Procedure SetFileSize(File$, length.q)
(** Truncates the named file to the given size. *)</lang>
Protected fh, pos, i
 
If FileSize(File$) < length
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:
Debug "File to small, is a directory or does not exist."
 
ProcedureReturn #False
<lang ocaml>val ftruncate : file_descr -> int -> unit
Else
(** Truncates the file corresponding to the given descriptor to the given size. *)</lang>
fh = OpenFile(#PB_Any, File$)
 
FileSeek(fh, length)
=={{header|Pascal}}==
TruncateFile(fh)
<lang pascal>
CloseFile(fh)
Program FileTruncate;
EndIf
 
ProcedureReturn #True
uses
EndProcedure</lang>
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.
</lang>
Output:
<pre>
File for truncation: test
Truncate position: 3
File "test" truncated at position 3.
</pre>
 
=={{header|Perl}}==
Line 227 ⟶ 256:
<lang PicoLisp>(de truncate (File Len)
(call "truncate" "-s" Len File) )</lang>
 
=={{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.
<lang PureBasic>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</lang>
 
=={{header|Python}}==
Anonymous user