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


相关推荐
敢敢のwings15 分钟前
捯饬DeepScaleR-1.5B----最有可能在嵌入端部署的思考模型
服务器·开发语言
EPSDA2 小时前
Linux命名管道与共享内存
linux·运维·服务器·开发语言·c++
flymyd2 小时前
Ubuntu 22.04完整安装Xinference及解决依赖报错
linux·运维·ubuntu·ai·llama
兔斯基灬木木2 小时前
Linux内核 - 非仿生机器人之感知主控系统(协议栈)
linux·机器人
superman超哥4 小时前
开源堡垒机 JumpServer 社区版实战教程:一步步构建企业安全运维环境
运维·开源·堡垒机·jumpserver·安全运维·社区版
游客5205 小时前
自动化办公|xlwings 数据类型和转换
运维·python·自动化
计算机毕设指导65 小时前
基于Spring Boot的医院挂号就诊系统【免费送】
java·服务器·开发语言·spring boot·后端·spring·maven
Jin·5 小时前
解决 DeepSeek 官网服务器繁忙的实用方案
运维·服务器·deepseek
Boxsc_midnight5 小时前
【用Deepseek搭建免费的个人知识库--综合教程(完整版)】第二篇:Ollama服务器
运维·服务器·人工智能·nginx
wtsolutions7 小时前
迅投QMT程序化交易系统-行情和交易服务器连接、中断和再连接
服务器·python