truncate(p1)
public
Truncates file to at most integer bytes. The file must be
opened for writing. Not available on all platforms.
f = File.new("out", "w")
f.syswrite("1234567890")
f.truncate(5)
f.close()
File.size("out")
Show source
static VALUE
rb_file_truncate(VALUE obj, VALUE len)
{
rb_io_t *fptr;
off_t pos;
long pos;
rb_secure(2);
pos = NUM2POS(len);
GetOpenFile(obj, fptr);
if (!(fptr->mode & FMODE_WRITABLE)) {
rb_raise(rb_eIOError, "not opened for writing");
}
rb_io_flush_raw(obj, 0);
if (ftruncate(fptr->fd, pos) < 0)
rb_sys_fail_path(fptr->pathv);
if (chsize(fptr->fd, pos) < 0)
rb_sys_fail_path(fptr->pathv);
return INT2FIX(0);
}