文件IO

1:思维导图

2:使用3语言编写一个简易的界面,界面如下 1:标准输出流 2:标准错误流 3:文件流 要求:按1的时候,通过printf输出数据,按2的时候,通过perror输出数据,按3的时候将输入写入文件中 同时通过dup2函数,将标准错误流重定向到错误日志,将文件流重定向到终端

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

		}
	}
	close(err);
	close(file);

	return 0;
}

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

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

		}
	}
	close(err);
	close(file);

	return 0;
}
相关推荐
Echo_NGC223744 分钟前
【神经视频编解码NVC】传统神经视频编解码完全指南:从零读懂 AI 视频压缩的基石
人工智能·深度学习·算法·机器学习·视频编解码
会员果汁1 小时前
leetcode-动态规划-买卖股票
算法·leetcode·动态规划
橘颂TA1 小时前
【剑斩OFFER】算法的暴力美学——二进制求和
算法·leetcode·哈希算法·散列表·结构与算法
地平线开发者3 小时前
征程 6 | cgroup sample
算法·自动驾驶
姓蔡小朋友3 小时前
算法-滑动窗口
算法
君义_noip4 小时前
信息学奥赛一本通 2134:【25CSPS提高组】道路修复 | 洛谷 P14362 [CSP-S 2025] 道路修复
c++·算法·图论·信息学奥赛·csp-s
kaikaile19954 小时前
基于拥挤距离的多目标粒子群优化算法(MO-PSO-CD)详解
数据结构·算法
不忘不弃4 小时前
求两组数的平均值
数据结构·算法
leaves falling4 小时前
迭代实现 斐波那契数列
数据结构·算法