c語(yǔ)言可以通過(guò)stat()函數(shù)獲得文件屬性,通過(guò)返回的文件屬性,從中獲取文件大小。
成都創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)網(wǎng)站建設(shè)與網(wǎng)站營(yíng)銷(xiāo),提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站開(kāi)發(fā)、seo優(yōu)化、網(wǎng)站排名、互聯(lián)網(wǎng)營(yíng)銷(xiāo)、重慶小程序開(kāi)發(fā)、公眾號(hào)商城、等建站開(kāi)發(fā),成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)策劃專(zhuān)家,為不同類(lèi)型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制解決方案,幫助客戶在新的全球化互聯(lián)網(wǎng)環(huán)境中保持優(yōu)勢(shì)。
#include
sys/stat.h
可見(jiàn)以下結(jié)構(gòu)體和函數(shù)
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
*);
//根據(jù)文件名得到文件屬性
參考代碼:
#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 );
}
1 函數(shù)都是獲取文件(普通文件,目錄,管道,socket,字符,塊()的屬性。
函數(shù)原型
#include sys/stat.h
int stat(const char *restrict pathname, struct stat *restrict buf);
提供文件名字,獲取文件對(duì)應(yīng)屬性。
int fstat(int filedes, struct stat *buf);
通過(guò)文件描述符獲取文件對(duì)應(yīng)的屬性。
int lstat(const char *restrict pathname, struct stat *restrict buf);
連接文件描述命,獲取文件屬性。
2 文件對(duì)應(yīng)的屬性
struct stat {
mode_t st_mode; //文件對(duì)應(yīng)的模式,文件,目錄等
ino_t st_ino; //inode節(jié)點(diǎn)號(hào)
dev_t st_dev; //設(shè)備號(hào)碼
dev_t st_rdev; //特殊設(shè)備號(hào)碼
nlink_t st_nlink; //文件的連接數(shù)
uid_t st_uid; //文件所有者
gid_t st_gid; //文件所有者對(duì)應(yīng)的組
off_t st_size; //普通文件,對(duì)應(yīng)的文件字節(jié)數(shù)
time_t st_atime; //文件最后被訪問(wèn)的時(shí)間
time_t st_mtime; //文件內(nèi)容最后被修改的時(shí)間
time_t st_ctime; //文件狀態(tài)改變時(shí)間
blksize_t st_blksize; //文件內(nèi)容對(duì)應(yīng)的塊大小
blkcnt_t st_blocks; //偉建內(nèi)容對(duì)應(yīng)的塊數(shù)量
};
可以通過(guò)上面提供的函數(shù),返回一個(gè)結(jié)構(gòu)體,保存著文件的信息。
main()
{
struct stat buf;
stat("當(dāng)前目錄下的文件名字",buf);
printf("%ld",buf.st_blksize);
}
C語(yǔ)言中獲取文件大小方式有很多,在不使用任何系統(tǒng)命令,僅使用C自身庫(kù)函數(shù)情況下,常用方式有兩種:
一、獲取文件系統(tǒng)屬性,讀取文件大小。
在C語(yǔ)言庫(kù)函數(shù)中有stat函數(shù),可以獲取文件的基本信息,其中就有文件大小。
#include?sys/stat.h//包含頭文件。
int?file_size(char*?filename)//獲取文件名為filename的文件大小。
{
struct?stat?statbuf;
int?ret;
ret?=?stat(filename,statbuf);//調(diào)用stat函數(shù)
if(ret?!=?0)?return?-1;//獲取失敗。
return?statbuf.st_size;//返回文件大小。
}
二、通過(guò)C語(yǔ)言文件操作,獲取文件大小。
以fopen打開(kāi)的文件,通過(guò)fseek可以定位到文件尾,這時(shí)使用ftell函數(shù),返回的文件指針偏移值,就是文件的實(shí)際大小。
代碼如下:
#include?stdio.h//包含頭文件。
int?file_size(char*?filename)//獲取文件名為filename的文件大小。
{
FILE?*fp?=?fopen(filename,?"rb");//打開(kāi)文件。
int?size;
if(fp?==?NULL)?//?打開(kāi)文件失敗
return?-1;
fseek(fp,?0,?SEEK_END);//定位文件指針到文件尾。
size=ftell(fp);//獲取文件指針偏移量,即文件大小。
fclose(fp);//關(guān)閉文件。
return?size;
}
三、注意事項(xiàng):
第一種方式為直接讀取文件信息,無(wú)需打開(kāi)文件,所以更高效。
四、測(cè)試代碼:
以上接口函數(shù),均可以用如下主函數(shù)測(cè)試:
#include?stdio.h
int?main()
{
char?s[100];
int?size;
scanf("%s",s);//輸入文件名
size?=?file_size(s);//獲取文件大小。
if(size?==?-1)?printf("無(wú)法獲取文件大小,可能文件并不存在或不可讀\n");
else?printf("文件大小為%d\n",?size);
return?0;
}
errno錯(cuò)誤代碼: ?
1?ENOENT?????????參數(shù)file_name指定的文件不存在????
2?ENOTDIR????????路徑中的目錄存在但卻非真正的目錄????
3?ELOOP??????????欲打開(kāi)的文件有過(guò)多符號(hào)連接問(wèn)題,上限為16符號(hào)連接????
4?EFAULT?????????參數(shù)buf為無(wú)效指針,指向無(wú)法存在的內(nèi)存空間????
5?EACCESS????????存取文件時(shí)被拒絕????
6?ENOMEM?????????核心內(nèi)存不足????
7?ENAMETOOLONG???參數(shù)file_name的路徑名稱(chēng)太長(zhǎng)
這里很可能是 4
stat(取得文件狀態(tài))
相關(guān)函數(shù) fstat,lstat,chmod,chown,readlink,utime
表頭文件 #include sys/stat.h
#include unistd.h
定義函數(shù) int stat(const char * file_name,struct stat *buf);
函數(shù)說(shuō)明 stat()用來(lái)將參數(shù)file_name所指的文件狀態(tài),復(fù)制到參數(shù)buf所指的結(jié)構(gòu)中。
下面是struct stat內(nèi)各參數(shù)的說(shuō)明
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 文件的設(shè)備編號(hào)
st_ino 文件的i-node
st_mode 文件的類(lèi)型和存取的權(quán)限
st_nlink 連到該文件的硬連接數(shù)目,剛建立的文件值為1。
st_uid 文件所有者的用戶識(shí)別碼
st_gid 文件所有者的組識(shí)別碼
st_rdev 若此文件為裝置設(shè)備文件,則為其設(shè)備編號(hào)
st_size 文件大小,以字節(jié)計(jì)算
st_blksize 文件系統(tǒng)的I/O 緩沖區(qū)大小。
st_blcoks 占用文件區(qū)塊的個(gè)數(shù),每一區(qū)塊大小為512 個(gè)字節(jié)。
st_atime 文件最近一次被存取或被執(zhí)行的時(shí)間,一般只有在用mknod、utime、read、write與tructate時(shí)改變。
st_mtime 文件最后一次被修改的時(shí)間,一般只有在用mknod、utime和write時(shí)才會(huì)改變
st_ctime i-node最近一次被更改的時(shí)間,此參數(shù)會(huì)在文件所有者、組、權(quán)限被更改時(shí)更新先前所描述的st_mode 則定義了下列數(shù)種情況
S_IFMT 0170000 文件類(lèi)型的位遮罩
S_IFSOCK 0140000 scoket
S_IFLNK 0120000 符號(hào)連接
S_IFREG 0100000 一般文件
S_IFBLK 0060000 區(qū)塊裝置
S_IFDIR 0040000 目錄
S_IFCHR 0020000 字符裝置
S_IFIFO 0010000 先進(jìn)先出
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 文件所有者具可讀取權(quán)限
S_IWUSR(S_IWRITE)00200 文件所有者具可寫(xiě)入權(quán)限
S_IXUSR(S_IEXEC) 00100 文件所有者具可執(zhí)行權(quán)限
S_IRGRP 00040 用戶組具可讀取權(quán)限
S_IWGRP 00020 用戶組具可寫(xiě)入權(quán)限
S_IXGRP 00010 用戶組具可執(zhí)行權(quán)限
S_IROTH 00004 其他用戶具可讀取權(quán)限
S_IWOTH 00002 其他用戶具可寫(xiě)入權(quán)限
S_IXOTH 00001 其他用戶具可執(zhí)行權(quán)限
上述的文件類(lèi)型在POSIX 中定義了檢查這些類(lèi)型的宏定義
S_ISLNK (st_mode) 判斷是否為符號(hào)連接
S_ISREG (st_mode) 是否為一般文件
S_ISDIR (st_mode)是否為目錄
S_ISCHR (st_mode)是否為字符裝置文件
S_ISBLK (s3e) 是否為先進(jìn)先出
S_ISSOCK (st_mode) 是否為socket
若一目錄具有sticky 位(S_ISVTX),則表示在此目錄下的文件只能被該文件所有者、此目錄所有者或root來(lái)刪除或改名。
返回值 執(zhí)行成功則返回0,失敗返回-1,錯(cuò)誤代碼存于errno
錯(cuò)誤代碼 ENOENT 參數(shù)file_name指定的文件不存在
ENOTDIR 路徑中的目錄存在但卻非真正的目錄
ELOOP 欲打開(kāi)的文件有過(guò)多符號(hào)連接問(wèn)題,上限為16符號(hào)連接
EFAULT 參數(shù)buf為無(wú)效指針,指向無(wú)法存在的內(nèi)存空間
EACCESS 存取文件時(shí)被拒絕
ENOMEM 核心內(nèi)存不足
ENAMETOOLONG 參數(shù)file_name的路徑名稱(chēng)太長(zhǎng)
范例 #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);
}
函數(shù)名: abort
功 能: 異常終止一個(gè)進(jìn)程
用 法: void abort(void);
程序例:
#include stdio.h
#include stdlib.h
int main(void)
{
printf("Calling abort()\n");
abort();
return 0; /* This is never reached */
}
函數(shù)名: abs
功 能: 求整數(shù)的絕對(duì)值
用 法: 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;
}
函數(shù)名: absread, abswirte
功 能: 絕對(duì)磁盤(pán)扇區(qū)讀、寫(xiě)數(shù)據(jù)
用 法: 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);
}
函數(shù)名: access
功 能: 確定文件的訪問(wèn)權(quán)限
用 法: 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);
}
函數(shù)名: acos
功 能: 反余弦函數(shù)
用 法: 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;
}
函數(shù)名: allocmem
功 能: 分配DOS存儲(chǔ)段
用 法: 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;
}
函數(shù)名: arc
功 能: 畫(huà)一弧線
用 法: 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;
}
函數(shù)名: asctime
功 能: 轉(zhuǎn)換日期和時(shí)間為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;
}
函數(shù)名: asin
功 能: 反正弦函數(shù)
用 法: 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);
}
函數(shù)名: assert
功 能: 測(cè)試一個(gè)條件并可能使程序終止
用 法: 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;
}
函數(shù)名: atan
功 能: 反正切函數(shù)
用 法: 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);
}
函數(shù)名: atan2
功 能: 計(jì)算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;
}
函數(shù)名: atexit
功 能: 注冊(cè)終止函數(shù)
用 法: 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;
}
函數(shù)名: atof
功 能: 把字符串轉(zhuǎn)換成浮點(diǎn)數(shù)
用 法: 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;
}
函數(shù)名: atoi
功 能: 把字符串轉(zhuǎn)換成長(zhǎng)整型數(shù)
用 法: 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;
}
函數(shù)名: atol
功 能: 把字符串轉(zhuǎn)換成長(zhǎng)整型數(shù)
用 法: 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);
}
本文標(biāo)題:c語(yǔ)言stat函數(shù)返回值,C語(yǔ)言stat函數(shù)
瀏覽路徑:http://chinadenli.net/article10/hddjdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、企業(yè)建站、網(wǎng)站改版、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)