truncate(p1, p2)
public
Truncates the file file_name to be at most integer bytes
long. Not available on all platforms.
f = File.new("out", "w")
f.write("1234567890")
f.close
File.truncate("out", 5)
File.size("out")
Show source
static VALUE
rb_file_s_truncate(VALUE klass, VALUE path, VALUE len)
{
off_t pos;
rb_secure(2);
pos = NUM2OFFT(len);
FilePathValue(path);
path = rb_str_encode_ospath(path);
if (truncate(StringValueCStr(path), pos) < 0)
rb_sys_fail(RSTRING_PTR(path));
{
int tmpfd;
if ((tmpfd = open(StringValueCStr(path), 0)) < 0) {
rb_sys_fail(RSTRING_PTR(path));
}
rb_update_max_fd(tmpfd);
if (chsize(tmpfd, pos) < 0) {
close(tmpfd);
rb_sys_fail(RSTRING_PTR(path));
}
close(tmpfd);
}
return INT2FIX(0);
}