Truncate a file: Difference between revisions
Content added Content deleted
(Haskell information) |
(ReSort alphabetically, {{header|Pascal}}: add example) |
||
Line 120: | Line 120: | ||
</lang> |
</lang> |
||
==Icon and {{header|Unicon}}== |
=={{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. |
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> |
<lang Unicon>truncate(f := open(filename, "bu"), newsizeinbytes) & close(f)</lang> |
||
Line 144: | Line 144: | ||
} |
} |
||
}</lang> |
}</lang> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
Line 194: | Line 182: | ||
</lang> |
</lang> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
=={{header|Pascal}}== |
|||
⚫ | |||
<lang pascal> |
|||
⚫ | |||
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. |
|||
</lang> |
|||
Output: |
|||
<pre> |
|||
File for truncation: test |
|||
Truncate position: 3 |
|||
File "test" truncated at position 3. |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Line 227: | Line 256: | ||
<lang PicoLisp>(de truncate (File Len) |
<lang PicoLisp>(de truncate (File Len) |
||
(call "truncate" "-s" Len File) )</lang> |
(call "truncate" "-s" Len File) )</lang> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
=={{header|Python}}== |
=={{header|Python}}== |