文件属性数字转rwx(c/c++)

本文介绍了一种使用位操作来生成文件权限字符串的方法。通过右移和按位与操作判断特定位置上的比特是否为1,进而决定插入字符'r'、'w'、'x'还是'-'到字符串中。这种方法常用于文件系统的权限设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以数字33188为例

string temp;

string::iterator it = temp.begin();
int i;
for ( i= 0; i < 9; i++)
{
if ((33188 >> i) & 0x1 )
{
if (0 == i % 2) 
{
temp.insert(it, 1, 'r');
}
else if ( 1 == i % 2)
{
temp.insert(it, 1, 'w');
}
else if ( 2 == i % 2)
{
temp.insert(it, 1, 'x');
}
}
else
{
temp.insert(it, 1, '-');
}

}
cout<<temp<<endl;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值