Linux学习_文件操作权限

文件权限操作

open在打开一个文件时可以传入文件的创建权限
反过来说,我们可以去获得并修改一个文件的权限

  1. 获得权限:

    • access函数,注意并不是获得完整的权限值,而是查看针对user的组别,是否有传入的权限
     #include <unistd.h>
    
       int access(const char *pathname, int mode);
    
    
    • 返回:拥有返回0,不拥有-1
    • 参数
      • pathname
      • mode:
        F_OK:文件是否存在
        R_OK:文件是否可读
        W_OK:文件是否可写
        X_OK:文件是否可执行
  2. 修改权限

    • chmod和fchmod函数
 #include <sys/stat.h>

       int chmod(const char *pathname, mode_t mode);
       int fchmod(int fd, mode_t mode);

  • chmod和fchmod函数区别:功能无区别,chmod传入文件路径,fchmod传入文件描述符

示例:

/*
 * 文件权限操作.c
 * 
 */

#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>

char* fn="demo1.txt";

int main(int argc, char **argv)
{
	chmod(fn,0000);
	int res=access(fn,R_OK);
	printf("文件可读?:%d\n",res);
	if(res==0)printf("该文件可读!\n");
	else 
	{printf("该文件不可读!\n");chmod(fn,0664);printf("chmod 664\n该文件已可读!\n");}
	return 0;
}

输出:

文件可读?:-1
该文件不可读!
chmod 664
 该文件已可读!

  1. 文件控制
#include <unistd.h>
#include <fcntl.h>

int fcntl(int fd, int cmd, ... /* arg */ );
//最后传入一个NULL代表可变参结束
  • 参数:
    • fd: 文件描述符
    • cmd: fcntl针对文件的具体操作方法
      F_GETFL:获取fd代表的文件属性
      F_SETFL:为fd代表的文件设置属性
      注意:当cmd传入的参数为F_SETFL时候,需要传入第三个参数设置属性
  • cmd具体如下:
 File descriptor flags

       F_GETFD (void)
              Return (as the function result) the file descriptor flags; arg is ignored.

       F_SETFD (int)
              Set the file descriptor flags to the value specified by arg.

 File status flags

       F_GETFL (void)
              Return (as the function result) the file access mode and the file status flags; arg is ignored.

       F_SETFL (int)
              Set  the file status flags to the value specified by arg.  File access mode (O_RDONLY, O_WRONLY, O_RDWR) and
              file creation flags (i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg are ignored.  On Linux,  this  command
              can  change  only  the  O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags.  It is not possible to
              change the O_DSYNC and O_SYNC flags; see BUGS, below.
  • 示例:
/*
 * 文件权限操作.c
 * 2秒scan一次,若有输入则输出输入,没有则no data
 */

#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>


int main(int argc, char **argv)
{
	char buf[128]={0};
	int res=0;
	int mode=fcntl(STDIN_FILENO,F_GETFL); //获取文件属性
	mode |= O_NONBLOCK; //增加O_NONBLOCK文件属性
	fcntl(STDIN_FILENO,F_SETFL,mode); //重新设置文件属性
	while(1){
	res=scanf("%s",buf);
	if(res==1){
		printf("receive data:%s\n",buf);
		res=0;
		sleep(2);
	}
	else{
		printf("no data\n");
		sleep(2);
	}
	}
	return 0;
}

  • 输出:
no data
no data
hello
receive data:hello
no data
world
receive data:world
no data
no data

练习

  1. printf输出属性:
/*
 * printf输出属性.c
 * 在输出文字上移动光标,'a'为向左,'c'为向右
 *  */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
	
	char str[20]="helloworld";
	char ctl[6]={"\033[6D"}; //'\033'为一个字符
	char ch;
	system("clear");
	printf("%s%s",str,ctl);	fflush(stdout);
	while(1){
		system("stty -icanon");
		system("stty -echo");
		ch=getchar();
		if(ch=='a') ++ctl[2];
		if(ch=='d') --ctl[2];
		system("stty echo");
		system("clear");
		printf("%s%s",str,ctl);
		fflush(stdout);
	}

	return 0;
}

  1. 开奖
/*
 * printf开奖.c
 * 四个球数字开奖
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <user.h> //自建库,包含getch()


int main(int argc, char **argv)
{
	int mode=fcntl(STDIN_FILENO,F_GETFL);
	mode |= O_NONBLOCK;
	fcntl(STDIN_FILENO,F_SETFL,mode);
	system("stty -icanon");
	system("stty -echo");
	int num=0;
	printf("键入回车开始开奖:\n");
	printf("__-__-__-__");
	
	while(getch()!='\n');
	
	printf("\033[11D"); //从左开始第11个是一行头
	fflush(stdout);
	for(int i=0;i<4;i++){
		while(1){
			num=rand()%100;
			printf("%2d",num);
			fflush(stdout);
			if(getch()=='\n'){ //为了防止在不是getch()的时候输出'\n',需要在前面设置stty
				if(i != 3) printf("-");
				fflush(stdout);
				break;
			}
			printf("\b\b");
		}
	}
	system("stty icanon");
	system("stty echo");
	sleep(2);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值