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")
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);
#ifdef HAVE_FTRUNCATE
if (ftruncate(fptr->fd, pos) < 0)
rb_sys_fail_path(fptr->pathv);
#else
# ifdef HAVE_CHSIZE
if (chsize(fptr->fd, pos) < 0)
rb_sys_fail(fptr->pathv);
# else
rb_notimplement();
# endif
#endif
return INT2FIX(0);
}