Truncate a file: Difference between revisions

Content added Content deleted
(+Java without throwing an error for a larger new size (cna be changed if that is really a hard requirement))
(Add Ruby.)
Line 1: Line 1:
{{draft task}}
{{draft task}} [[Category:File System Operations]]


The task is to demonstrate how to 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). The 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, if no other method is possible. 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 and exit.
The task is to demonstrate how to 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). The 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, if no other method is possible. 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 and exit.

=={{header|Java}}==
=={{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).
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).
Line 21: Line 22:
}
}
}</lang>
}</lang>

=={{header|PureBasic}}==
=={{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.
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.
Line 36: Line 38:
ProcedureReturn #True
ProcedureReturn #True
EndProcedure</lang>
EndProcedure</lang>
{{omit from|GUISS}}


=={{header|Ruby}}==
[[Category:File System Operations]]
This only works with some platforms. If truncation is not available, then Ruby raises NotImplementedError.

<lang ruby># Open a file for writing, and truncate it to 1234 bytes.
File.open("file", "ab") { |f| f.truncate(1234) }

# Trunate a file to 567 bytes.
File.truncate("file", 567)</lang>

{{omit from|GUISS}}