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;
rb_secure(2);
pos = NUM2OFFT(len);
GetOpenFile(obj, fptr);
if (!(fptr->mode & FMODE_WRITABLE)) {
rb_raise(rb_eIOError, "not opened for writing");
}
rb_io_flush(obj);
if (ftruncate(fptr->fd, pos) < 0)
rb_sys_fail_path(fptr->pathv);
if (chsize(fptr->fd, pos) < 0)
rb_sys_fail(fptr->pathv);
rb_notimplement();
return INT2FIX(0);
}