如何用C语言查看文件属性

Python012

如何用C语言查看文件属性,第1张

已修改答复,请试验~~

如果实在linux下,你可以用stat()函数查看,Windows下我不知道函数,通常我是在linux下做的。。

不过在Windows下,你可以使用

需要包含

#include

system("attrib"

+

szFileName)

看看,szFileName是你的文件名,写入

双引号

内~

比如文件叫a.c

你就

system("attrib

a.c")

即可~

c语言可以通过stat()函数获得文件属性,通过返回的文件属性,从中获取文件大小

#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 )

}