stat函数

本文介绍了如何使用_stat函数来获取文件的详细信息,包括文件大小、所在驱动器及最后修改时间。通过一个示例程序展示了_stat函数的具体应用,帮助读者理解其功能和用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     今天看程序时,看到了stat这个函数,特此备份一下,转载百度百科:

     函数名: stat()

  功 能: 得到文件的信息

  用 法: int _stat(const char *path,struct _stat *buffer)

  程序例:

  // 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 );

  }

  }

  输出结果:

  File size : 732

  Drive : C:

  Time modified : Thu Feb 07 14:39:36 2002

转载于:https://www.cnblogs.com/xiaoliyu/archive/2009/02/25/1397723.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值