Linux标准IOday3

1:思维导图

2 :使用3语言编写一个简易的界面,界面如下
1:标准输出流
2:标准错误流
3:文件流

要求:按1的时候,通过printf输出数据,按2的时候,通过perror输出数据,按3的时候将输入写入文件中,同时通过dup2函数,将标准错误流重定向到错误日志,将文件流重定向到终端

复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
	int err=open("err.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);
	int file=open("1.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);
	char arr[10]="haha";
	while(1)
	{
		printf("1.标准输出流\n");
		printf("2.标准错误流\n");
		printf("3.文件流\n");
		int n=0;
		scanf("%d",&n);
		while(getchar()!=10);
		switch(n)
		{
			case 1:
				printf("std_out\n");
				break;
			case 2:
				perror("std_err\n");
				break;
			case 3:
				write(file,arr,strlen(arr));
				dup2(err,2);
				dup2(1,file);
				break;
			default:
				return 1;
		}
	}
	close(err);
	close(file);
	return 0;
}

3:使用stat函数判断一个文件是否存在 同组人可执行 权限,如果存在则去除该权限,如果不存在则追加该权限,自己想办法查询 更改文件权限的函数是什么

复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
 #include <sys/types.h>
       #include <sys/stat.h>

int main(int argc, const char *argv[])
{
	if(argc < 2)
	{
		printf("请输入文件名\n");
	}
	int fd = open(argv[1],O_WRONLY |O_CREAT|O_TRUNC);
	struct stat buf={0};
	stat(argv[1],&buf);
	perror("stat:");
	mode_t mode = buf.st_mode;
	if((mode | 0010) == mode)
	{
		mode &= ~(0010);
	}
	else
	{
		mode |=0010;
	}
	fchmod(fd,mode);
	close(fd);
	return 0;
}


相关推荐
序属秋秋秋23 分钟前
《Linux系统编程之进程控制》【进程等待】
linux·c语言·c++·进程·系统编程·进程控制·进程等待
zfj32128 分钟前
top 命令中的 wa (IO wait) 指标,理论上几乎完全是由磁盘IO(包括swap)引起的,而不是网络IO
linux·网络·top·iowait
Xの哲學33 分钟前
Linux网卡注册流程深度解析: 从硬件探测到网络栈
linux·服务器·网络·算法·边缘计算
不哦罗密经39 分钟前
python相关
服务器·前端·python
数字化转型202544 分钟前
SAP Signavio 在风机制造行业的深度应用研究
大数据·运维·人工智能
用户6135411460161 小时前
libicu-62.1-6.ky10.x86_64.rpm 安装步骤详解(麒麟V10系统)
linux
WordPress学习笔记1 小时前
wordpress根据分类ID调用分类名称和分类描述
运维·wordpress
qq_455760851 小时前
docker - 镜像、存储卷和网络深入理解
运维·docker·容器
2501_945837432 小时前
云服务器的技术本质与发展演进
服务器
秋4272 小时前
防火墙基本介绍与使用
linux·网络协议·安全·网络安全·架构·系统安全