stat获取文件属性+原创测试程序

      stat, fstat, lstat - get file status
      
----------------------------------------------------
      
    #include <sys/types.h>
      
    #include <sys/stat.h>
      
    #include <unistd.h>
      
      
    int stat (const char *path, struct stat      *buf);
      
    int      fstat(int      filedes, struct stat *buf);
      
    int lstat(const char *path, struct stat      *buf);
      
      
    lstat 函数与 stat 类似,但是当文件是一个符号连接时, lstat 返回该符号连接的有关信息,而不是由该符号连接引用的文件的信息。
      struct--stat
      
----------------------------------------------------
      
struct stat {
      
    mode_t         st_mode;    // file type & mode(permissions)
      
    ino_t          st_ino;     // i-node number(serial number)
      
    dev_t          st_dev;     // device number(filesystem)
      
    dev_t          st_rdev;    // device number for specials files
      
    nlink_t   st_nlink;        // number of links
      
    uid_t          st_uid;     // user ID of owner
      
    gid_t          st_gid;     // group ID of owner
      
    off_t          st_size;    // size in bytes, for regular files
      
    time_t         st_atime;   // time of last access
      
    time_t         st_mtime;   // time of last modification
      
    time_t         st_ctime;   // time of last file status change
      
    long           st_blksize; // best I/O block size
      
    long           st_blocks; -// number of      512-byte blocks allocated
      
};

      

文件类型标志包括:

S_IFBLK:文件是一个特殊的块设备

S_IFDIR:文件是一个目录

S_IFCHR:文件是一个特殊的字符设备

S_IFIFO:文件是一个FIFO设备

S_IFREG:文件是一个普通文件(REG即使regular啦)

S_IFLNK:文件是一个符号链接


//实验一:简单stat获取文件属性
#include<stdio.h>    
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#define FILENAME "3.txt"

int main(void)
{
	
	int ret=-1;
	struct stat buf;
	
	memset(&buf,0,sizeof(buf));// memset后buf中全是0
	ret= stat(FILENAME, &buf);//stat后buf中包含了3.txt文件的各种信息
	 
    if(buf<0)
	 {
		perror("fail");
		_exit(-1);
	 }
	// 成功获取了stat结构体,从中可以得到各种属性信息了
	printf("inod= %d.\n",buf.st_ino);
	printf("size = %d bytes.\n", buf.st_size);
	printf("st_blksize = %d.\n", buf.st_blksize);
	
	
	return 0;
	
}

*/

//***************************************************************************************************

//实验二:文件属性/权限测试
/*
#include<stdio.h>  
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#define FILENAME "3.txt"

int main(void)
{
	
	int ret=-1;
	struct stat buf;
	
	memset(&buf,0,sizeof(buf));// memset后buf中全是0
	ret= stat(FILENAME, &buf);//stat后buf中包含了3.txt文件的各种信息
	 
    if(ret<0)
	 {
		perror("fail");
		_exit(-1);
	 }
	// 成功获取了stat结构体,从中可以得到各种属性信息了
	printf("成功获取stat结构体.\n");
	
 //	文件属性的获取
 #if 1
  int result=S_ISDIR(buf.st_mode);
  
  if(result==0)
  {
	printf("result = %d\n", result);
  } 
	 
#endif	


//文件权限的获取
#if 0
  unsigned int result=((buf.st_mode&S_IRUSR)?1:0);
  printf("file owner,result=%u.\n",result);
#endif
  return 0;
}
*/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值