监测文件属性

stat结构体
  struct stat finfo;
  stat( sFileName, &finfo );
  int size = finfo.st_size;
  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; //伟建内容对应的块数量
  };

下面的程序是实时监测文件属性的

void main( void ){
	struct stat buf;
	static time_t mtime=0;
	for(;;){
		if(stat("main.cpp", &buf ) != -1 ) { 
			if (buf.st_mtime != mtime ) {
				mtime = buf.st_mtime;
				/* Output some of the statistics: */
				printf( "File size     : %ld\n", buf.st_size );
				printf( "Drive         : %c:\n", buf.st_dev + 'A' );
				printf( "Time modified : %s", ctime( &buf.st_atime ) );
				printf( "Time modified : %ld\n", buf.st_atime  ); 
			}	 
		}
	}	
}

另一个程序

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>


int main(void){
	struct stat buf;
	if(stat( "main.cpp", &buf ) != -1 ) {    
		printf( "Time modified : %ld\n",buf.st_mtime);
		printf( "Time modified : %s",ctime(&buf.st_mtime));  
	}
	return 0; 
 }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值