Linux 改變文件大小的方法
發(fā)布日期:2022-04-14 11:40 | 文章來源:源碼之家
函數(shù)原型:
#include <unistd.h> int ftruncate(int fd, off_t length); //改變文件大小為length指定大小;返回值 執(zhí)行成功則返回0,失敗返回-1。
函數(shù)ftruncate會將參數(shù)fd指定的文件大小改為參數(shù)length指定的大小。參數(shù)fd為已打開的文件描述詞,而且必須是以寫入模式打開的文件。如果原來的文件大小比參數(shù)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;
}

總結(jié)
以上所述是小編給大家介紹的Linux 改變文件大小的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對本站網(wǎng)站的支持!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信