struct stat 的用法
2015-08-16 21:04
381 查看
//! 需要包含de头文件
#include <sys/types.h>
#include <sys/stat.h>
int stat(const char *filename, struct stat *buf); //! prototype,原型
struct stat
{
dev_t st_dev; /* ID of device containing file -文件所在设备的ID*/
ino_t st_ino; /* inode number -inode节点号*/
mode_t st_mode; /* protection -保护模式?*/
nlink_t st_nlink; /* number of hard links -链向此文件的连接数(硬连接)*/
uid_t st_uid; /* user ID of owner -user id*/
gid_t st_gid; /* group ID of owner - group id*/
dev_t st_rdev; /* device ID (if special file) -设备号,针对设备文件*/
off_t st_size; /* total size, in bytes -文件大小,字节为单位*/
blksize_t st_blksize; /* blocksize for filesystem I/O -系统块的大小*/
blkcnt_t st_blocks; /* number of blocks allocated -文件所占块数*/
time_t st_atime; /* time of last access -最近存取时间*/
time_t st_mtime; /* time of last modification -最近修改时间*/
time_t st_ctime; /* time of last status change - */
};
#include <iostream>
#include <ctime>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
int
main ()
{
struct stat buf;
int result;
result = stat ("./Makefile", &buf);
if (result != 0)
{
perror ("Failed ^_^");
}
else
{
//! 文件的大小,字节为单位
cout << "size of the file in bytes: " << buf.st_size << endl;
//! 文件创建的时间
cout << "time of creation of the file: " << ctime (&buf.st_ctime) <<
endl;
//! 最近一次修改的时间
cout << "time of last modification of the file: " <<
ctime (&buf.st_mtime) << endl;
//! 最近一次访问的时间
cout << "time of last access of the file: " << ctime (&buf.st_atime)
<< endl;
}
return 0;
}
$ ./test
size of the file in bytes: 36
time of creation of the file: Sun May 24 18:38:10 2009
time of last modification of the file: Sun May 24 18:38:10 2009
time of last access of the file: Sun May 24 18:38:13 2009
#include <sys/types.h>
#include <sys/stat.h>
int stat(const char *filename, struct stat *buf); //! prototype,原型
struct stat
{
dev_t st_dev; /* ID of device containing file -文件所在设备的ID*/
ino_t st_ino; /* inode number -inode节点号*/
mode_t st_mode; /* protection -保护模式?*/
nlink_t st_nlink; /* number of hard links -链向此文件的连接数(硬连接)*/
uid_t st_uid; /* user ID of owner -user id*/
gid_t st_gid; /* group ID of owner - group id*/
dev_t st_rdev; /* device ID (if special file) -设备号,针对设备文件*/
off_t st_size; /* total size, in bytes -文件大小,字节为单位*/
blksize_t st_blksize; /* blocksize for filesystem I/O -系统块的大小*/
blkcnt_t st_blocks; /* number of blocks allocated -文件所占块数*/
time_t st_atime; /* time of last access -最近存取时间*/
time_t st_mtime; /* time of last modification -最近修改时间*/
time_t st_ctime; /* time of last status change - */
};
#include <iostream>
#include <ctime>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
int
main ()
{
struct stat buf;
int result;
result = stat ("./Makefile", &buf);
if (result != 0)
{
perror ("Failed ^_^");
}
else
{
//! 文件的大小,字节为单位
cout << "size of the file in bytes: " << buf.st_size << endl;
//! 文件创建的时间
cout << "time of creation of the file: " << ctime (&buf.st_ctime) <<
endl;
//! 最近一次修改的时间
cout << "time of last modification of the file: " <<
ctime (&buf.st_mtime) << endl;
//! 最近一次访问的时间
cout << "time of last access of the file: " << ctime (&buf.st_atime)
<< endl;
}
return 0;
}
$ ./test
size of the file in bytes: 36
time of creation of the file: Sun May 24 18:38:10 2009
time of last modification of the file: Sun May 24 18:38:10 2009
time of last access of the file: Sun May 24 18:38:13 2009
相关文章推荐
- Java 对象
- hdu 1874 畅通工程续 dijsktra dijkstra+邻接表 优先队列 bellman-ford bellman-ford队列优化(基础题目,一步步优化)
- hdu 5137 How Many Maos Does the Guanxi Worth(dijkstra && floyd)
- lr_eval_string函数在订飞机票中的例子-返回脚本中参数的值
- android中一个解决办法
- Java多重循环算法问题:(九九乘法表、打印平行四边形、打印空心菱形、打印菱形、搬砖问题、斐波那契数列、猴子吃桃问题、回文数、1!+2!+...+20!)
- 再看“陈伟视频”理解多态机制 ——你还记得“橘子,苹果的例子么”
- 07 映射和集合类型 - 《Python 核心编程》
- 自定义spring schema简化与canal集成
- Python解析器源码加密系列之(二):一次使用标准c的FILE*访问内存块的尝试
- JavaEE SSH集成框架(两) struts2 本地加载dtd文件,action组态
- ES6 新特性
- python2.7版本 xlutils库下载
- java中IO流学习小结
- 小白书之二叉树指针法构建二叉树以及二叉树先序遍历
- hdu 1212 Big Number 大数取模
- Liunx下安装Apache mysql pHp服务
- 最长上升子序列 LIS(Longest Increasing Subsequence)(转)
- window.open的小技巧分享
- XML读写