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


相关推荐
酷熊代理29 分钟前
软路由如何屏蔽国外IP?RouterOS保姆级实战教程(附自动化脚本)
运维·网络协议·tcp/ip·自动化·智能路由器·软路由·小猫pptp
tanyongxi6637 分钟前
Linux 中 Git 使用指南:从零开始掌握版本控制
linux·git
UpUpUp……1 小时前
Linux中Gdb调试工具常用指令大全
linux·运维·服务器·笔记
cxy_61 小时前
nginx中间件部署
运维·nginx·中间件
程序员buddha1 小时前
最节省服务器,手搓电子证书查询系统
运维·服务器
若云止水1 小时前
ngx_conf_read_token
linux·运维·服务器
Sliphades1 小时前
C# Exe + Web 自动化 (BitComet 绿灯 自动化配置、设置)
运维·自动化
重生之我在20年代敲代码1 小时前
Linux工具【1】(编辑器vim、编译器gcc与g++)
linux·服务器·编辑器
眠りたいです2 小时前
Linux:利用System V系列的-共享内存,消息队列实现进程间通信
linux·运维·服务器·c++·进程间通信
Acrel136519467382 小时前
光伏储能:未来能源的黄金搭档
运维·能源