Logo

Programming-Idioms

  • C++
  • Perl

Idiom #199 Truncate a file at the current file position

Truncate a file F at the given file position.

use Path::Tiny qw(path);
my $F = path('F')->openrw;
# ... read some from $F to advance
# position of file handle (not shown) ...
truncate $F, tell $F;
using System.IO;
var F = new FileStream("F", FileMode.Open)
// advance into F here
F.SetLength(F.Position);

Assumes code reads F to advance position into file stream.

New implementation...
< >
Bart