S_ISREG等几个常见的宏

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;  
}
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路漫漫其远,吾求索

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值