日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux truncate文件,linux系统编程:用truncate调整文件大小

發布時間:2023/12/4 linux 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux truncate文件,linux系统编程:用truncate调整文件大小 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

truncate的使用非常簡單:

int truncate(const char *path, off_t length);

參數1:文件名

參數2:? 文件需要被調整的大小

length 大于 文件大小, 文件后面會填充空白字節或者空洞

length 小于 文件大小, 文件多出的部分,會被舍棄

源代碼:

1 /*================================================================

2 * Copyright (C) 2018 . All rights reserved.

3 *

4 * 文件名稱:trunc.c

5 * 創 建 者:ghostwu(吳華)

6 * 創建日期:2018年01月11日

7 * 描 述:調整文件大小

8 *

9 ================================================================*/

10

11 #include

12 #include

13 #include

14 #include

15 #include

16 #include

17

18 int main(int argc, char *argv[])

19 {

20 if( argc < 3 || strcmp( argv[1], "--help" ) == 0 ) {

21 printf( "usage:%s filename s\n", argv[0] );

22 exit( -1 );

23 }

24

25 if( argv[2][0] != 's' ) {

26 printf( "設置文件的大小,需要用s開頭\n" );

27 exit( -1 );

28 }

29

30 char* endptr;

31 long int len = strtol( &argv[2][1], &endptr, 10 );

32 if( len == LONG_MIN || len == LONG_MAX ) {

33 printf( "參數轉換失敗\n" );

34 exit( -1 );

35 }

36

37 truncate( argv[1], len );

38

39 return 0;

40 }

View Code

完整的測試:

ghostwu@ubuntu:~/c_program/tlpi/chapter5$ ls -l test.txt

-rw-rw-r-- 1 ghostwu ghostwu 410 1月 11 16:09 test.txt

ghostwu@ubuntu:~/c_program/tlpi/chapter5$ ./trunc test.txt s500

ghostwu@ubuntu:~/c_program/tlpi/chapter5$ ls -l test.txt

-rw-rw-r-- 1 ghostwu ghostwu 500 1月 11 16:38 test.txt

ghostwu@ubuntu:~/c_program/tlpi/chapter5$ vim test.txt

ghostwu@ubuntu:~/c_program/tlpi/chapter5$ ./trunc test.txt 300

設置文件的大小,需要用s開頭

ghostwu@ubuntu:~/c_program/tlpi/chapter5$ ./trunc test.txt s300

ghostwu@ubuntu:~/c_program/tlpi/chapter5$ ls -l test.txt

-rw-rw-r-- 1 ghostwu ghostwu 300 1月 11 16:38 test.txt

總結

以上是生活随笔為你收集整理的linux truncate文件,linux系统编程:用truncate调整文件大小的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。