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.常见问题
服务器·华为·ai·harmonyos·鸿蒙·智能体
天空'之城2 分钟前
Linux 系统编程 22:五种 IO 模型全解
linux
小张成长计划..1 小时前
【Linux】10:冯·诺依曼体系结构和操作系统
linux·运维·服务器
悦儿遥遥雨15 小时前
PXE + Kickstart 无人值守批量部署系统
linux·javascript·nginx
m0_564876845 小时前
没有公网IP的情况下,怎么进行远程连接两个电脑
服务器·tcp/ip·负载均衡
Imagine Miracle5 小时前
【WSL】让WSL2后台持久运行不自动关闭的解决方案
linux·windows·wsl
兔C5 小时前
Linux 命令行入门学习资料 day_2
linux·运维·服务器
eggcode6 小时前
Linux命令基础与操作技巧
linux
多巴胺梦想家6 小时前
事务与并发控制:当多人同时操作数据库
服务器·数据库·oracle
二宝哥7 小时前
VMware Workstation 实战:CentOS 7.9 安装、桥接网络配置与克隆管理详解
linux·centos·vmware