I/O 作业3

1、文件拷贝、错误日志

#include <allhead.h>
int main(int argc, const char *argv[])
{
	int efd=open("./err.txt",O_WRONLY | O_CREAT | O_TRUNC,0664);

	dup2(efd,STDERR_FILENO);
	int fd1=open(argv[1],O_RDONLY);
	//perror("被拷贝文件打开状态:");
	int fd2=open(argv[2],O_WRONLY | O_CREAT | O_TRUNC,0664);
	perror("拷贝文件打开状态:");
	char buf[20]={0};
	while(1)
	{
		int res=read(fd1,buf,1);
		perror("读取文件状态:");

		int res1=write(fd2,buf,1);
		perror("写入文件状态:");

		if(res1==-1){break;}
		if(res==0){break;}
	}
	close(fd2);
	close(fd1);


	return 0;
}

2、判断权限进行更改

#include <allhead.h>
int main(int argc, const char *argv[])
{
	struct stat mode;
    char str[100]="ls -l ";
	strcat(str,argv[1]);
	int res=stat(argv[1],&mode);
	printf("%d\n",mode.st_mode);
	if((S_IWUSR | mode.st_mode) == mode.st_mode)
	{printf("有可写权限\n");
		chmod(argv[1],mode.st_mode&(~S_IWUSR));
		printf("可写权限已清除\n");
		system(str);
	}
	else
	{
		printf("没有可写权限\n");
		chmod(argv[1],mode.st_mode|(S_IWUSR));
		printf("已添加可写权限\n");
		system(str);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值