利用程式檢視檔案的大小 lseek函式

2021-10-06 11:33:07 字數 992 閱讀 1532

在linux中有一句真理:一切皆為檔案

首先來留意乙個有用的函式 lseek。它就可以來檢視檔案的長度。

#include

#include

off_t lseek

(int fd,off_t offset,

int whence);*

***

其中:引數offset的解釋與引數whence的值有關

1.若whence是seek_set,則將該檔案的位移量設定為距檔案開始處offset個位元組

2.若whence是seek_cur,則將該檔案的位移量設定為其當前值offset個位元組,offset可為正或負

3.若whence是seek_end,則將該檔案的位移量設定為檔案長度加offset個位元組,offset可為正或負。

char

* pathname =

"know.txt"

;int fd =

open

(pathname,o_rdwr|o_creat|o_trunc,

0644);

if(fd ==-1

)char

* str =

"hello world\n"

;int bytes =

write

(fd,str,

strlen

(str));

if(bytes <0)

off_t surrpos =0;

surrpos =

lseek

(fd,0,

seek_set);

printf

("surrpos:%d\n"

,surrpos)

;

在程式中,可以把whence設定為seek_end,偏移0個位元組。得到的surrpos的值對視檔案的長度。

檢視檔案大小

du sh sort n 統計當前資料夾 目錄 大小,並按檔案大小排序 附送 du sk filename檢視指定檔案大小 linux ls以k m g為單位檢視檔案大小 linux ls以k m g為單位檢視檔案大小。man ls h,human readable print sizes in h...

檢視檔案大小du sh ll h

檢視資料夾總大小 root iz9488z4qfcz sztoyon du sh 974m root iz9488z4qfcz sztoyon ll h total 516m rwxrwxrwx 1 root root 23 may 25 12 32 1.php drwxrwxrwx 83 root...

linux檢視檔案大小

stat filepath xanarry thinkpad stat downloads jdk 8u60 linux x64 tar.gz file home xanarry downloads jdk 8u60 linux x64.tar.gz size 181238643 blocks 35...