struct stat结构体的详解和用法
2015-07-15 21:06
288 查看
[cpp] view plaincopy
//! 需要包含de头文件
#include <sys/types.h>
#include <sys/stat.h>
S_ISLNK(st_mode):是否是一个连接.
S_ISREG(st_mode):是否是一个常规文件.
S_ISDIR(st_mode):是否是一个目录
S_ISCHR(st_mode):是否是一个字符设备.
S_ISBLK(st_mode):是否是一个块设备
S_ISFIFO(st_mode):是否 是一个FIFO文件.
S_ISSOCK(st_mode):是否是一个SOCKET文件
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 - */
};
[cpp] view plaincopy
#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;
}
[cpp] view plaincopy
$ ./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
//! 需要包含de头文件
#include <sys/types.h>
#include <sys/stat.h>
S_ISLNK(st_mode):是否是一个连接.
S_ISREG(st_mode):是否是一个常规文件.
S_ISDIR(st_mode):是否是一个目录
S_ISCHR(st_mode):是否是一个字符设备.
S_ISBLK(st_mode):是否是一个块设备
S_ISFIFO(st_mode):是否 是一个FIFO文件.
S_ISSOCK(st_mode):是否是一个SOCKET文件
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 - */
};
[cpp] view plaincopy
#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;
}
[cpp] view plaincopy
$ ./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
相关文章推荐
- bzoj1149
- [leedcode 85] Maximal Rectangle
- Request To JavaBean(请求对象转换为JavaBean对象)
- 基数排序
- java EE之jsp注释、声明以及输出jsp表达式 复习
- Uva10859 - Placing Lampposts
- Linux 内存管理之 mmap 解析(二)
- POJ 1837 Balance (多重背包计数)
- bzoj1148
- DraggableGridView可拖动的GridView
- 要看的书单
- bzoj1145
- Objective-C中的@property和@synthesize用法
- 怎么计算两个日期之间的差值
- hdu2199
- ubuntu 重新安装mysql
- POJ 3615 Cow Hurdles //Floyd算法
- Chrome谷歌浏览器如何禁用JavaScript
- iOS中的c第二天分支语句
- bzoj1146