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;
}


相关推荐
嵌入式×边缘AI:打怪升级日志1 天前
Linux 驱动与应用开发核心自测题库(面试官问答完整版)
linux·运维·php
eastyuxiao1 天前
企业 OpenClaw 文档自动化落地项目 - 思维导图范围拆解
运维·自动化
薛定谔的悦1 天前
储能充放电状态机执行逻辑详解
linux·数据库·能源·储能·bms
Sirens.1 天前
Umami:从Cloud迁移到服务器
运维·服务器
练习时长一年1 天前
分页插件冲突问题
服务器·前端·windows
zhangfeng11331 天前
CI/CD 是软件开发中的两个核心实践,合起来指代一套自动化的软件交付流程
运维·ci/cd·自动化
春蕾夏荷_7282977251 天前
2、c++ acl tcp服务器客户端简单实例-服务器端(1)
服务器·c++·tcp/ip
嵌入式×边缘AI:打怪升级日志1 天前
Tina SDK Linux Kernel 基本使用(实战篇:为7寸RGB LCD触摸屏添加驱动支持).md
linux·运维·服务器
十 一 丶1 天前
如何在客户端实现ssh的免密登录?
运维·rust·ssh
想唱rap1 天前
应用层HTTPS协议
服务器·网络·c++·网络协议·http·https