思维导图
使用3语言编写一个简易的界面
界面如下 1:标准输出流 2:标准错误流 3:文件流
要求:
按1的时候,通过printf输出数据,
按2的时候,通过perror输出数据,
按3的时候将输入写入文件中 同时通过dup2函数,将标准错误流重定向到错误日志,将文件流重定向到终端
代码
#include <stdio.h>#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>int main(int argc, const char *argv[])
{int num=-1;char buf[20]=""; //用于 写入文本//打开错误日志int fp_log=open("log.txt",O_CREAT|O_WRONLY,0666);int fp_file=open("happy",O_CREAT|O_WRONLY,0666);int ERROR=dup(2); //备份 标准错误流int STDOUT=dup(1); //备份 标准输出流int STDIN=dup(0); //备份 标准输入流int FP_FILE=dup(fp_file); //备份 标准输入流while(num!=5){printf("1.标准输出 2.标准错误流 3.文件流 4.恢复标准错误流重定向 5退出\n");printf("请选择:");scanf("%d",&num);while(getchar()!='\n');{ switch(num){case 1:printf("TIPS:标准输入流:未被重定向时,应在终端显示\n\n");break;case 2:printf("TIPS:标准输入流:重定向后, 于log文件中查看效果\n");perror("TIPS:标准错误流:重定向后,本语句不应在终端显示\n");printf("\n");break;case 3:printf("TIPS:只见到此语句 说明重定向发生 于happy文本中查看输出内容\n\n");dup2(FP_FILE,1); //重定向:printf内容输入到文本printf("TIPS:本条语句应出现在 错误日志 文件中\n\n");fflush(stdout); //清空缓存区:数据写入到文本dup2(STDOUT,1); //恢复重定向fflush(stdout);dup2(fp_log,2); //重定向: 错误流 重定向到 错误日志fflush(stderr);break;case 4:printf("TIPS:恢复 标准错误流 重定向\n\n");dup2(ERROR,2); //恢复重定向fflush(stderr);}}}return 0;
}
3重定向效果
使用stat函数判断一个文件是否存在 同组人可执行 权限,如果存在则去除该权限,如果不存在则追加该权限 自己想办法查询 更改文件权限的函数是什么