获取文件信息 _tstat

在vs2008的msdn Index中搜索stat,推荐使用_tstat

 

可以得到一系列Get status information on a file.函数

 

说一下个人用这些函数的作用吧

 

1.得到文件信息

 

这是这个函数最基本的作用了,不细说,msdn上面说的很清楚

 

这是msdn中的实例

// crt_stat.c
// This program uses the _stat function to
// report information about the file named crt_stat.c.
 
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>

int main( void )
{
   struct _stat buf;
   int result;
   char timebuf[26];
   char* filename = "crt_stat.c";
   errno_t err;

   // Get data associated with "crt_stat.c": 
   result = _stat( filename, &buf );

   // Check if statistics are valid: 
   if( result != 0 )
   {
      perror( "Problem getting information" );
      switch (errno)
      {
         case ENOENT:
           printf("File %s not found./n", filename);
           break;
         case EINVAL:
           printf("Invalid parameter to _stat./n");
           break;
         default:
           /* Should never be reached. */
           printf("Unexpected error in _stat./n");
      }
   }
   else
   {
      // Output some of the statistics: 
      printf( "File size     : %ld/n", buf.st_size );
      printf( "Drive         : %c:/n", buf.st_dev + 'A' );
      err = ctime_s(timebuf, 26, &buf.st_mtime);
      if (err)
      {
         printf("Invalid arguments to ctime_s.");
         exit(1);
      }
      printf( "Time modified : %s", timebuf );
   }
}

 

2.判断一个路径是文件还是文件夹

 

最开始的时候我是为了这个目的在csdn上面搜索到这个函数的

 

使用方法也很简单,只是判断返回的struct中的st_mode,一个语句即可

 

if(stat.st_mode & _S_IFDIR) // 条件为真表示是文件夹注意是用&符号而不是==也不是&&(我刚开始就写成了&&,⊙﹏⊙b汗)

 

 

3.其他比较重要的作用个人还没有碰到,欢迎留言补充,谢谢

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值