body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;} th{border: 1px solid gray; padding: 4px; background-color: #DDD;} td{border: 1px solid gray; padding: 4px;} tr:nth-child(2n){background-color: #f8f8f8;}
函数原型:
#include <unistd.h>
int ftruncate(int fd, off_t length); //改变文件大小为length指定大小;返回值 执行成功则返回0,失败返回-1。
函数ftruncate会将参数fd指定的文件大小改为参数length指定的大小。参数fd为已打开的文件描述词,而且必须是以写入模式打开的文件。如果原来的文件大小比参数length大,则超过的部分会被删去。
|
test.c | |
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<strings.h>
#include<stdio.h>
int main(int argc,char** argv)
{
struct stat st;
bzero(&st,sizeof(st));
stat(argv[1],&st);
printf("%s %ld\n",argv[1],st.st_size);
int fd=open(argv[1],O_RDWR);
ftruncate(fd,20);
bzero(&st,sizeof(st));
stat(argv[1],&st);
printf("%s %ld\n",argv[1],st.st_size);
close(fd);
return 0;
}
|