资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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://cdkjz.cn/article/hddjdo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220