c語言stat函數返回值,C語言stat函數

c語言如何通過文件屬性獲取文件大小知道

c語言可以通過stat()函數獲得文件屬性,通過返回的文件屬性,從中獲取文件大小。

成都創新互聯致力于互聯網網站建設與網站營銷,提供成都做網站、成都網站設計、成都外貿網站建設、網站開發、seo優化、網站排名、互聯網營銷、重慶小程序開發、公眾號商城、等建站開發,成都創新互聯網站建設策劃專家,為不同類型的客戶提供良好的互聯網應用定制解決方案,幫助客戶在新的全球化互聯網環境中保持優勢。

#include

sys/stat.h

可見以下結構體和函數

struct

stat

{

_dev_t

st_dev;

_ino_t

st_ino;

unsigned

short

st_mode;

short

st_nlink;

short

st_uid;

short

st_gid;

_dev_t

st_rdev;

_off_t

st_size;

//文件大小

time_t

st_atime;

time_t

st_mtime;

time_t

st_ctime;

};

stat(const

char

*,

struct

_stat

*);

//根據文件名得到文件屬性

參考代碼:

#include sys/stat.h

void main( )

{

struct stat buf ;

if ( stat( "test.txt", buf ) 0 )

{

perror( "stat" );

return ;

}

printf("file size:%d\n", buf.st_size );

}

c語言高手們請問一下-stat這個怎么用行么?

1 函數都是獲取文件(普通文件,目錄,管道,socket,字符,塊()的屬性。

函數原型

#include sys/stat.h

int stat(const char *restrict pathname, struct stat *restrict buf);

提供文件名字,獲取文件對應屬性。

int fstat(int filedes, struct stat *buf);

通過文件描述符獲取文件對應的屬性。

int lstat(const char *restrict pathname, struct stat *restrict buf);

連接文件描述命,獲取文件屬性。

2 文件對應的屬性

struct stat {

mode_t st_mode; //文件對應的模式,文件,目錄等

ino_t st_ino; //inode節點號

dev_t st_dev; //設備號碼

dev_t st_rdev; //特殊設備號碼

nlink_t st_nlink; //文件的連接數

uid_t st_uid; //文件所有者

gid_t st_gid; //文件所有者對應的組

off_t st_size; //普通文件,對應的文件字節數

time_t st_atime; //文件最后被訪問的時間

time_t st_mtime; //文件內容最后被修改的時間

time_t st_ctime; //文件狀態改變時間

blksize_t st_blksize; //文件內容對應的塊大小

blkcnt_t st_blocks; //偉建內容對應的塊數量

};

可以通過上面提供的函數,返回一個結構體,保存著文件的信息。

main()

{

struct stat buf;

stat("當前目錄下的文件名字",buf);

printf("%ld",buf.st_blksize);

}

如何用C語言獲取文件的大小

C語言中獲取文件大小方式有很多,在不使用任何系統命令,僅使用C自身庫函數情況下,常用方式有兩種:

一、獲取文件系統屬性,讀取文件大小。

在C語言庫函數中有stat函數,可以獲取文件的基本信息,其中就有文件大小。

#include?sys/stat.h//包含頭文件。

int?file_size(char*?filename)//獲取文件名為filename的文件大小。

{

struct?stat?statbuf;

int?ret;

ret?=?stat(filename,statbuf);//調用stat函數

if(ret?!=?0)?return?-1;//獲取失敗。

return?statbuf.st_size;//返回文件大小。

}

二、通過C語言文件操作,獲取文件大小。

以fopen打開的文件,通過fseek可以定位到文件尾,這時使用ftell函數,返回的文件指針偏移值,就是文件的實際大小。

代碼如下:

#include?stdio.h//包含頭文件。

int?file_size(char*?filename)//獲取文件名為filename的文件大小。

{

FILE?*fp?=?fopen(filename,?"rb");//打開文件。

int?size;

if(fp?==?NULL)?//?打開文件失敗

return?-1;

fseek(fp,?0,?SEEK_END);//定位文件指針到文件尾。

size=ftell(fp);//獲取文件指針偏移量,即文件大小。

fclose(fp);//關閉文件。

return?size;

}

三、注意事項:

第一種方式為直接讀取文件信息,無需打開文件,所以更高效。

四、測試代碼:

以上接口函數,均可以用如下主函數測試:

#include?stdio.h

int?main()

{

char?s[100];

int?size;

scanf("%s",s);//輸入文件名

size?=?file_size(s);//獲取文件大小。

if(size?==?-1)?printf("無法獲取文件大小,可能文件并不存在或不可讀\n");

else?printf("文件大小為%d\n",?size);

return?0;

}

c語言stat函數

errno錯誤代碼: ?

1?ENOENT?????????參數file_name指定的文件不存在????

2?ENOTDIR????????路徑中的目錄存在但卻非真正的目錄????

3?ELOOP??????????欲打開的文件有過多符號連接問題,上限為16符號連接????

4?EFAULT?????????參數buf為無效指針,指向無法存在的內存空間????

5?EACCESS????????存取文件時被拒絕????

6?ENOMEM?????????核心內存不足????

7?ENAMETOOLONG???參數file_name的路徑名稱太長

這里很可能是 4

C語言如何獲取文件信息?stat這個函數如何使用? - C / C++ -

stat(取得文件狀態)

相關函數 fstat,lstat,chmod,chown,readlink,utime

表頭文件 #include sys/stat.h

#include unistd.h

定義函數 int stat(const char * file_name,struct stat *buf);

函數說明 stat()用來將參數file_name所指的文件狀態,復制到參數buf所指的結構中。

下面是struct stat內各參數的說明

struct stat

{

dev_t st_dev; /*device*/

ino_t st_ino; /*inode*/

mode_t st_mode; /*protection*/

nlink_t st_nlink; /*number of hard links */

uid_t st_uid; /*user ID of owner*/

gid_t st_gid; /*group ID of owner*/

dev_t st_rdev; /*device type */

off_t st_size; /*total size, in bytes*/

unsigned long st_blksize; /*blocksize for filesystem I/O */

unsigned long st_blocks; /*number of blocks allocated*/

time_t st_atime; /* time of lastaccess*/

time_t st_mtime; /* time of last modification */

time_t st_ctime; /* time of last change */

};

st_dev 文件的設備編號

st_ino 文件的i-node

st_mode 文件的類型和存取的權限

st_nlink 連到該文件的硬連接數目,剛建立的文件值為1。

st_uid 文件所有者的用戶識別碼

st_gid 文件所有者的組識別碼

st_rdev 若此文件為裝置設備文件,則為其設備編號

st_size 文件大小,以字節計算

st_blksize 文件系統的I/O 緩沖區大小。

st_blcoks 占用文件區塊的個數,每一區塊大小為512 個字節。

st_atime 文件最近一次被存取或被執行的時間,一般只有在用mknod、utime、read、write與tructate時改變。

st_mtime 文件最后一次被修改的時間,一般只有在用mknod、utime和write時才會改變

st_ctime i-node最近一次被更改的時間,此參數會在文件所有者、組、權限被更改時更新先前所描述的st_mode 則定義了下列數種情況

S_IFMT 0170000 文件類型的位遮罩

S_IFSOCK 0140000 scoket

S_IFLNK 0120000 符號連接

S_IFREG 0100000 一般文件

S_IFBLK 0060000 區塊裝置

S_IFDIR 0040000 目錄

S_IFCHR 0020000 字符裝置

S_IFIFO 0010000 先進先出

S_ISUID 04000 文件的(set user-id on execution)位

S_ISGID 02000 文件的(set group-id on execution)位

S_ISVTX 01000 文件的sticky位

S_IRUSR(S_IREAD) 00400 文件所有者具可讀取權限

S_IWUSR(S_IWRITE)00200 文件所有者具可寫入權限

S_IXUSR(S_IEXEC) 00100 文件所有者具可執行權限

S_IRGRP 00040 用戶組具可讀取權限

S_IWGRP 00020 用戶組具可寫入權限

S_IXGRP 00010 用戶組具可執行權限

S_IROTH 00004 其他用戶具可讀取權限

S_IWOTH 00002 其他用戶具可寫入權限

S_IXOTH 00001 其他用戶具可執行權限

上述的文件類型在POSIX 中定義了檢查這些類型的宏定義

S_ISLNK (st_mode) 判斷是否為符號連接

S_ISREG (st_mode) 是否為一般文件

S_ISDIR (st_mode)是否為目錄

S_ISCHR (st_mode)是否為字符裝置文件

S_ISBLK (s3e) 是否為先進先出

S_ISSOCK (st_mode) 是否為socket

若一目錄具有sticky 位(S_ISVTX),則表示在此目錄下的文件只能被該文件所有者、此目錄所有者或root來刪除或改名。

返回值 執行成功則返回0,失敗返回-1,錯誤代碼存于errno

錯誤代碼 ENOENT 參數file_name指定的文件不存在

ENOTDIR 路徑中的目錄存在但卻非真正的目錄

ELOOP 欲打開的文件有過多符號連接問題,上限為16符號連接

EFAULT 參數buf為無效指針,指向無法存在的內存空間

EACCESS 存取文件時被拒絕

ENOMEM 核心內存不足

ENAMETOOLONG 參數file_name的路徑名稱太長

范例 #include sys/stat.h

#include unistd.h

mian()

{

struct stat buf;

stat (“/etc/passwd”,buf);

printf(“/etc/passwd file size = %d /n”,buf.st_size);

}

求C語言函數大全

函數名: abort

功 能: 異常終止一個進程

用 法: void abort(void);

程序例:

#include stdio.h

#include stdlib.h

int main(void)

{

printf("Calling abort()\n");

abort();

return 0; /* This is never reached */

}

函數名: abs

功 能: 求整數的絕對值

用 法: int abs(int i);

程序例:

#include stdio.h

#include math.h

int main(void)

{

int number = -1234;

printf("number: %d absolute value: %d\n", number, abs(number));

return 0;

}

函數名: absread, abswirte

功 能: 絕對磁盤扇區讀、寫數據

用 法: int absread(int drive, int nsects, int sectno, void *buffer);

int abswrite(int drive, int nsects, in tsectno, void *buffer);

程序例:

/* absread example */

#include stdio.h

#include conio.h

#include process.h

#include dos.h

int main(void)

{

int i, strt, ch_out, sector;

char buf[512];

printf("Insert a diskette into drive A and press any key\n");

getch();

sector = 0;

if (absread(0, 1, sector, buf) != 0)

{

perror("Disk problem");

exit(1);

}

printf("Read OK\n");

strt = 3;

for (i=0; i80; i++)

{

ch_out = buf[strt+i];

putchar(ch_out);

}

printf("\n");

return(0);

}

函數名: access

功 能: 確定文件的訪問權限

用 法: int access(const char *filename, int amode);

程序例:

#include stdio.h

#include io.h

int file_exists(char *filename);

int main(void)

{

printf("Does NOTEXIST.FIL exist: %s\n",

file_exists("NOTEXISTS.FIL") ? "YES" : "NO");

return 0;

}

int file_exists(char *filename)

{

return (access(filename, 0) == 0);

}

函數名: acos

功 能: 反余弦函數

用 法: double acos(double x);

程序例:

#include stdio.h

#include math.h

int main(void)

{

double result;

double x = 0.5;

result = acos(x);

printf("The arc cosine of %lf is %lf\n", x, result);

return 0;

}

函數名: allocmem

功 能: 分配DOS存儲段

用 法: int allocmem(unsigned size, unsigned *seg);

程序例:

#include dos.h

#include alloc.h

#include stdio.h

int main(void)

{

unsigned int size, segp;

int stat;

size = 64; /* (64 x 16) = 1024 bytes */

stat = allocmem(size, segp);

if (stat == -1)

printf("Allocated memory at segment: %x\n", segp);

else

printf("Failed: maximum number of paragraphs available is %u\n",

stat);

return 0;

}

函數名: arc

功 能: 畫一弧線

用 法: void far arc(int x, int y, int stangle, int endangle, int radius);

程序例:

#include graphics.h

#include stdlib.h

#include stdio.h

#include conio.h

int main(void)

{

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

int midx, midy;

int stangle = 45, endangle = 135;

int radius = 100;

/* initialize graphics and local variables */

initgraph(gdriver, gmode, "");

/* read result of initialization */

errorcode = graphresult(); /* an error occurred */

if (errorcode != grOk)

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1); /* terminate with an error code */

}

midx = getmaxx() / 2;

midy = getmaxy() / 2;

setcolor(getmaxcolor());

/* draw arc */

arc(midx, midy, stangle, endangle, radius);

/* clean up */

getch();

closegraph();

return 0;

}

函數名: asctime

功 能: 轉換日期和時間為ASCII碼

用 法: char *asctime(const struct tm *tblock);

程序例:

#include stdio.h

#include string.h

#include time.h

int main(void)

{

struct tm t;

char str[80];

/* sample loading of tm structure */

t.tm_sec = 1; /* Seconds */

t.tm_min = 30; /* Minutes */

t.tm_hour = 9; /* Hour */

t.tm_mday = 22; /* Day of the Month */

t.tm_mon = 11; /* Month */

t.tm_year = 56; /* Year - does not include century */

t.tm_wday = 4; /* Day of the week */

t.tm_yday = 0; /* Does not show in asctime */

t.tm_isdst = 0; /* Is Daylight SavTime; does not show in asctime */

/* converts structure to null terminated

string */

strcpy(str, asctime(t));

printf("%s\n", str);

return 0;

}

函數名: asin

功 能: 反正弦函數

用 法: double asin(double x);

程序例:

#include stdio.h

#include math.h

int main(void)

{

double result;

double x = 0.5;

result = asin(x);

printf("The arc sin of %lf is %lf\n", x, result);

return(0);

}

函數名: assert

功 能: 測試一個條件并可能使程序終止

用 法: void assert(int test);

程序例:

#include assert.h

#include stdio.h

#include stdlib.h

struct ITEM {

int key;

int value;

};

/* add item to list, make sure list is not null */

void additem(struct ITEM *itemptr) {

assert(itemptr != NULL);

/* add item to list */

}

int main(void)

{

additem(NULL);

return 0;

}

函數名: atan

功 能: 反正切函數

用 法: double atan(double x);

程序例:

#include stdio.h

#include math.h

int main(void)

{

double result;

double x = 0.5;

result = atan(x);

printf("The arc tangent of %lf is %lf\n", x, result);

return(0);

}

函數名: atan2

功 能: 計算Y/X的反正切值

用 法: double atan2(double y, double x);

程序例:

#include stdio.h

#include math.h

int main(void)

{

double result;

double x = 90.0, y = 45.0;

result = atan2(y, x);

printf("The arc tangent ratio of %lf is %lf\n", (y / x), result);

return 0;

}

函數名: atexit

功 能: 注冊終止函數

用 法: int atexit(atexit_t func);

程序例:

#include stdio.h

#include stdlib.h

void exit_fn1(void)

{

printf("Exit function #1 called\n");

}

void exit_fn2(void)

{

printf("Exit function #2 called\n");

}

int main(void)

{

/* post exit function #1 */

atexit(exit_fn1);

/* post exit function #2 */

atexit(exit_fn2);

return 0;

}

函數名: atof

功 能: 把字符串轉換成浮點數

用 法: double atof(const char *nptr);

程序例:

#include stdlib.h

#include stdio.h

int main(void)

{

float f;

char *str = "12345.67";

f = atof(str);

printf("string = %s float = %f\n", str, f);

return 0;

}

函數名: atoi

功 能: 把字符串轉換成長整型數

用 法: int atoi(const char *nptr);

程序例:

#include stdlib.h

#include stdio.h

int main(void)

{

int n;

char *str = "12345.67";

n = atoi(str);

printf("string = %s integer = %d\n", str, n);

return 0;

}

函數名: atol

功 能: 把字符串轉換成長整型數

用 法: long atol(const char *nptr);

程序例:

#include stdlib.h

#include stdio.h

int main(void)

{

long l;

char *str = "98765432";

l = atol(lstr);

printf("string = %s integer = %ld\n", str, l);

return(0);

}

本文標題:c語言stat函數返回值,C語言stat函數
瀏覽路徑:http://m.kartarina.com/article10/hddjdo.html

成都網站建設公司_創新互聯,為您提供網站策劃外貿網站建設品牌網站設計企業建站網站改版虛擬主機

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

外貿網站制作
主站蜘蛛池模板: 无码专区—VA亚洲V天堂| 无码天堂亚洲国产AV| 亚洲av无码国产精品色午夜字幕 | 久久午夜无码免费| 人妻少妇偷人精品无码| 国产精品白浆无码流出| 日韩乱码人妻无码中文字幕久久| 97无码免费人妻超级碰碰夜夜| 中文无码日韩欧免费视频| 中文字幕亚洲精品无码| 亚洲一区爱区精品无码| 成人免费无码大片a毛片软件| 亚洲国产精品无码成人片久久| 夫妻免费无码V看片| 69ZXX少妇内射无码| 亚洲av无码乱码国产精品fc2| 亚洲av无码专区国产不乱码| 色综合久久中文字幕无码| 无码人妻精品一区二| 无码里番纯肉h在线网站| 久久人妻无码中文字幕| 亚洲AV无码一区二区三区DV | 无码精品人妻一区二区三区免费| 无码人妻少妇久久中文字幕蜜桃| aⅴ一区二区三区无卡无码| 无码国产精品一区二区免费| 无码人妻久久一区二区三区免费| 亚洲精品国产日韩无码AV永久免费网 | 无码人妻久久一区二区三区免费| 免费a级毛片无码a∨免费软件| 国产成人无码av在线播放不卡| 亚洲中文字幕久久精品无码A| 乱色精品无码一区二区国产盗| 亚洲精品无码永久在线观看你懂的| 亚洲中久无码不卡永久在线观看| 无码八A片人妻少妇久久| 久久久久亚洲AV无码专区桃色| 韩国无码AV片在线观看网站| 国产精品无码久久四虎| 精品无码综合一区二区三区| 精品无码人妻一区二区三区18|