【转】windows c++获取文件信息——_stat函数的使用

_stat函数的功能

_stat函数用来获取指定路径的文件或者文件夹的信息。

函数声明

int _stat(
   const char *path,
   struct _stat *buffer
);

参数:

path——文件或者文件夹的路径

buffer——获取的信息保存在内存中

返回值:

正确——返回0

错误——返回-1,具体错误码保存在errno

struct _stat结构体

_stat结构体是文件(夹)信息的结构体,定义如下:

struct stat {
        _dev_t     st_dev;        //文件所在磁盘驱动器号
        _ino_t     st_ino;        //inode,FAT、NTFS文件系统无意义
        unsigned short st_mode;   //文件、文件夹的标志
        short      st_nlink;      //非NTFS系统上通常为1
        short      st_uid;        //UNIX系统上为userid,windows上为0
        short      st_gid;        //UNIX系统上为groupid,windows上为0
        _dev_t     st_rdev;       //驱动器号,与st_dev相同
        _off_t     st_size;       //文件字节数
        time_t st_atime;          //上次访问时间
        time_t st_mtime;          //上次修改时间
        time_t st_ctime;          //创建时间
        };

以上信息就是可以通过_stat函数获取的所有相关信息,一般情况下,我们关心文件大小和创建时间、访问时间、修改时间。

例子

注:该例子来自MSDN,http://msdn.microsoft.com/en-us/library/14h5k7ff.aspx

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

支持不同时间长度和文件长度

_stat函数中时间定义为64位,文件长度也定义为32位,同时使用char*表示文件名称。我们知道可以时间可以定义为64位和32位:__time64和__time32,文件名也可以用宽字符来表示,文件长度也可以定义为64位。因此该函数有很多变体,这些函数用法都是一样的,我们根据具体情况选择使用哪个函数。

int _stat(
   const char *path,
   struct _stat *buffer
);
int _stat32(
   const char *path,
   struct __stat32 *buffer
);
int _stat64(
   const char *path,
   struct __stat64 *buffer
);
int _stati64(
   const char *path,
   struct _stati64 *buffer
);
int _stat32i64(str
   const char *path,
   struct _stat32i64 *buffer
);
int _stat64i32(str
   const char *path,
   struct _stat64i32 *buffer
);
int _wstat(
   const wchar_t *path,
   struct _stat *buffer
);
int _wstat32(
   const wchar_t *path,
   struct __stat32 *buffer
);
int _wstat64(
   const wchar_t *path,
   struct __stat64 *buffer
);
int _wstati64(
   const wchar_t *path,
   struct _stati64 *buffer
);
int _wstat32i64(
   const wchar_t *path,
   struct _stat32i64 *buffer
);
int _wstat64i32(
   const wchar_t *path,
   struct _stat64i32 *buffer
);
时间: 2024-08-11 01:34:04

【转】windows c++获取文件信息——_stat函数的使用的相关文章

【转】linux C++ 获取文件信息 stat函数详解

stat函数讲解 表头文件:    #include <sys/stat.h>             #include <unistd.h>定义函数:    int stat(const char *file_name, struct stat *buf);函数说明:    通过文件名filename获取文件信息,并保存在buf所指的结构体stat中返回值:      执行成功则返回0,失败返回-1,错误代码存于errno 错误代码:    ENOENT         参数fi

stat()函数--------------获取文件信息

stat():用于获取文件的状态信息,使用时需要包含<sys/stat.h>头文件. 函数原型:int stat(const char *path, struct stat *buf): struct stat { dev_t st_dev;         /* ID of device containing file */ ino_t st_ino;    /* inode number */ mode_t st_mode;  /* protection */ nlink_t st_nli

第九篇:使用 lstat 函数获取文件信息

前言 在之前的文章中,描述过如何用 fcntl 函数改变文件的状态标记.但,文件还有很多信息,如文件类型,权限设置,设备编号,访问时间等等.如果要获取这些信息,则使用函数 lstat 可以轻松达到这个目的. 下面的程序将使用该函数获取文件类型信息并判断它的类型. 但在这之前,先要说明文件信息结构体和文件类型判定宏. 文件信息结构体 调用 lstat 函数将会把指定文件的信息存放到 stat 结构体中,这个结构体的定义大致如下: 文件类型判定宏 文件结构体中的文件类型字段是一个整数,对此,可以使用

php 获取文件信息相关基础函数

<?phpheader('content-type:text/html;charset=utf-8');date_default_timezone_set('PRC');/** * 文件信息相关API */$filename="./test1.txt";// $filename="test";//filetype($filename):获取文件的类型,返回的是文件的类型echo '文件类型为:',filetype($filename),'<br/>

Linux获取文件信息

项目中需要对文件进行处理并分析,首先需要根据要求找到该文件,比如最后修改的文件 代码实现: 1 #include <unistd.h> 2 #include <sys/stat.h> 3 #include <time.h> 4 5 #include <iostream> 6 7 using namespace std; 8 9 int main(int argc, char **argv) 10 { 11 struct stat STFile; 12 sta

HttpWebRequest / HttpWebResponse 远程获取文件信息

HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(path+filename); //创建一个请求(获得需要的文件信息) HttpWebResponse myRes = (HttpWebResponse)myReq.GetResponse(); string gorlseftDate = myRes.LastModified.ToString("yyyy-MM-dd hh:mm:ss");//获得dataServer.ra

linux stat系统调用,获取文件信息。

stat 函数原型: int stat(const char *path, struct stat *buf); struct stat 说明 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

node 的fs.state 获取文件信息

1. fs.stat()可以获取文件的信息,用法如下: const fs = require('fs'); fs.stat('./book.js',(err,stats)=>{ if(err) throw err; // 可以利用此处判断文件是否存在,不存在会报err. console.log(stats.isFIle())//判断是否为文件 console.log(stats.isDirectory())//判断是否为文件夹 console.log(stats) //打印文件相关信息 }) 原

使用JavaAPI获取文件信息

package demo; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.BlockLocation; import org.apache.hadoop.fs.FileS