Truncate a file: Difference between revisions

From Rosetta Code
Content added Content deleted
m (extending the file is permitted if a suitable note is made)
m (after first deleting the original file)
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, 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. 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.


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

Revision as of 20:55, 19 July 2011

Truncate a file is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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.

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). <lang java>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(); } }</lang>

PureBasic

PureBasic has the internal function 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>

Ruby

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) }

  1. Trunate a file to 567 bytes.

File.truncate("file", 567)</lang>

UNIX Shell

<lang bash># Truncate a file named "myfile" to 1440 kilobytes. truncate -s 1440k myfile</lang>