2.23 read,sturct stat读取文件权限信息

#include"head.h"
// vsp xxx
int main(int argc, const char *argv[])
{
    struct stat buf;
    if(stat("./1.c",&buf)<0)
    {
        perror("stat");
        return -1;
    }
    
//见 man 2 stat


//文件类型以及权限    
    printf("mode:0%o\n",buf.st_mode);
    
# define MODE buf.st_mode //定义一个 图省事
int a=0400;
int i;
char str[10]="";
    for(i=0;i<9;i++)
    {
        if((a&MODE)!=0) //这里要括号,否则有优先级问题,先算MODE!=0得1或者0,再与a
        {
            switch (i%3)
            {
                case 0:str[i]='r';break;
                case 1:str[i]='w';break;
                case 2:str[i]='x';break;
            }
        }
        else str[i]='-';
        a=a>>1;
    }                
    puts(str);
}
#ifndef __head_h__
#define __head_h__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>




#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值