Truncate a file: Difference between revisions

Content added Content deleted
m (after first deleting the original file)
m (Re-word slightly, add note about facilities that don't change the file when a larger truncated size is given.)
Line 1: Line 1:
{{draft task}} [[Category:File System Operations]]
{{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, after first deleting the original file, 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. On some systems, the provided file truncation facilities also extend the file, if the specified length is greater than the current length of the file. For the purpose of this task, it is permissible for such facilities to be used. A note should made against the provided solution if an increase in length occurs and optionally a warning message relating to the increase in file size may be implemented.
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, after first deleting the original file, 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. On some systems, the provided file truncation facilities also extend the file, if the specified length is greater than the current length of the file. For the purpose of this task, it is permissible for such facilities to be used. If an increase in length occurs, it should be notes with the example. Optionally a warning message relating to the increase in file size may be implemented. In some languages, the provided truncation facilities might not change the file in this case. This should aslo be noted with the example.


=={{header|Java}}==
=={{header|Java}}==