_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[];
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 != )
{
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, , &buf.st_mtime);
if (err)
{
printf("Invalid arguments to ctime_s.");
exit();
}
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
);

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

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

    stat函数讲解 表头文件:    #include <sys/stat.h>             #include <unistd.h>定义函数:    int stat ...

  2. PHP常用获取文件路径的函数集合整理

    转自: http://blog.sina.com.cn/s/blog_71ed1b870102vslg.html 我们在开发PHP项目过程中,经常会用到包含文件,所以有时候需要获取文件的相对路径或者绝 ...

  3. FastDFS 通过文件名获取文件信息

    /** * 获取文件信息 * * param string group_name 文件所在的组名 * param string file_id 文件id(如: M00/09/BE/rBBZolgj6O ...

  4. C# -- 使用FileInfo获取文件信息

    C# -- 使用FileInfo获取文件信息 1. 代码实现 static void Main(string[] args) { GetFileInfo(@"D:\Test.xlsx&quo ...

  5. C#中获取文件信息的代码

    如下的内容内容是关于C#中获取文件信息的内容,应该对大伙有一些好处. FileInfo fi = new FileInfo(@"C:file.txt"); if(fi.Exists ...

  6. C#开发BIMFACE系列6 服务端API之获取文件信息

    在<C#开发BIMFACE系列4 服务端API之源上传文件>.<C#开发BIMFACE系列5 服务端API之文件直传>两篇文章中详细介绍了如何将本地文件上传到BIMFACE服务 ...

  7. C#开发BIMFACE系列7 服务端API之获取文件信息列表

    系列目录     [已更新最新开发文章,点击查看详细] 本文详细介绍如何获取BIMFACE平台中所有上传过的文件信息列表. 请求地址:GET https://file.bimface.com/file ...

  8. 拖放获取文件信息的bat代码

    参考:岁月如歌-通过拖曳获取文件信息的bat代码 拖放获取文件信息的bat代码 使用命令行配合7z解压文件时由于每次解压的文件不同,因此搜索了一下拖放识别文件信息的方法,以此方式来减轻工作量 获取文件 ...

  9. 使用QFileInfo类获取文件信息(在NTFS文件系统上,出于性能考虑,文件的所有权和权限检查在默认情况下是被禁用的,通过qt_ntfs_permission_lookup开启和操作。absolutePath()必须查询文件系统。而path()函数,可以直接作用于文件名本身,所以,path() 函数的运行会更快)

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Amnes1a/article/details/65444966QFileInfo类为我们提供了系统无 ...

随机推荐

  1. 编辑器笔记——sublime text3 编译sass

    如已经安装sass 和 sass build两个插件到ST,command+b编译也没有问题,那么另外安装koala,在koala中引入你要编辑的sass,并把该sass文件设置为自动编译,那么用ST ...

  2. ViewPager的简单例子

    这个例子是按照官网上的例子写的,有点抄袭的嫌疑,但是自己单独写一下会加深自己的印象. 首先是MainAcitivity.xml: <LinearLayout xmlns:android=&quo ...

  3. Codevs 2296 仪仗队 2008年省队选拔赛山东

    2296 仪仗队 2008年省队选拔赛山东 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题解 题目描述 Description 作为体育委员,C君负责这次运动 ...

  4. ubuntu windows 双系统 磁盘乱搞 grub 导致 error:no such partition grub rescue>

    乱搞分区磁盘什么的导致开机和这位仁兄一样:http://zhidao.baidu.com/question/495602654256708364.html?ssid=0&from=100187 ...

  5. 阅读《Oracle内核技术揭秘》的读书笔记

    阅读<Oracle内核技术揭秘>,对oracle的内存结构.锁.共享池.undo.redo等整理成了如下的思维导图:

  6. Linux C 程序 指针和字符串函数(11)

    指向字符串的指针 C语言访问字符串很多方法:1.用字符数组存放一个字符串 char string[] = "Linux C"; printf("%s\n".st ...

  7. Swing组件Jtree,JTablePane选项卡运用

    今天开始写技术博客,说实话,本没有什么技术,说是总结也好,说是分享也罢,总之是想自己有意识的做一些事情,作为一名即将毕业的大学生,总是想以最好的状态,去面向社会,今天就是我准备好了的时候,本人将技术博 ...

  8. sqlite3简单使用

    下载SQLite3 地址:http://www.sqlite.org/download.html 下载好的文档是SQlite3.exe,假如放在D盘. cmd D: D:\>SQlite3.ex ...

  9. This bison version is not supported for regeneration of the Zend/PHP parsers

    在 mac 中编译 php-src.git 报错: configure: WARNING: This bison version is not supported , excluded: ). con ...

  10. window2003安全设置

    1.    网上邻居->右键 属性->本地连接 右键属性->Microsoft网络的文件和打印机共享去掉选中   (影响端口: 139,445) 2.    禁止ADMIN$缺省共享 ...