您的位置:首页 > 其它

S_ISREG等几个常见的宏 struct stat

2015-09-12 11:10 369 查看
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文件

man 2 stat 可以查到stat数据结构,其中的st_mode就是上面几个宏的输入参数
struct stat {
dev_t st_dev;
ino_t st_ino;
mode_t st_mode;
nlink_t st_nlink;
uid_t st_uid;
gid_t st_gid;
dev_t st_rdev;
off_t st_size;
blksize_t st_blksize;
blkcnt_t st_blocks;
time_t st_mtime;
time_t st_ctime;
};

#ifndef WIN32
#include <unistd.h>
#endif
#include<sys/types.h>
#include<sys/stat.h>
#include <Windows.h>
#define  S_ISDIR(model) ((model) & _S_IFDIR)
int main()
{
int i;
struct stat buf;
char * ptr;
int result;
result = stat("C:/Users/10192647.ZTE/Downloads", &buf);
if (S_ISDIR(buf.st_mode))
ptr="目录";
cout<<"文件为:"<<ptr<<endl;
return 0;
}


#include <iostream>
#include <ctime>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
int  main4 ()
{
struct stat buf;
int result;
result = stat ("C:/out.txt", &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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: