stdout stderr 重定向到文件

1. stdout/stderr 重定向

1.1. dup/dup2 重定向到已打开文件 或 新文件

// https://www.man7.org/linux/man-pages/man2/dup.2.html
#include <unistd.h>
// 给 oldfd 分配一个新的描述符,返回
int dup(int oldfd);
// 给 oldfd 分配一个新的描述符 newfd,如果 newfd 已经打开,则先关闭
// 原来只有 oldfd 一个指针,现在有2个指针执行那个句柄对应的文件啦
int dup2(int oldfd, int newfd);
#include <stdio.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int mOldStdout = -1;
int mNewStdout = -1;

// stdout 输出 到文件中
void dupOutput(char * filename)
{
    mNewStdout = open(filename, O_CREAT|O_WRONLY|O_APPEND|O_SYNC, S_IRWXU|S_IRWXG|S_IROTH);
    mOldStdout = dup(STDOUT_FILENO);

    fsync(STDOUT_FILENO);
    dup2(mNewStdout, STDOUT_FILENO); // STDOUT_FILENO 指向 mNewStdout 文件,stdout 输出就会写到文件里
}

// 还原 stdout 到 命令窗口
void restoreOutput()
{
    fsync(STDOUT_FILENO);

    dup2(mOldStdout, STDOUT_FILENO);
    close(mOldStdout);
    close(mNewStdout);
}

int main(int argc, char * argv[]) {
    printf("IN STDOUT-1\n");

    dupOutput("logfile");
    printf("IN LOGFILE-1\n");

    restoreOutput();
    printf("IN STDOUT-2\n");
}
#include <stdio.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

static FILE *logHandel = NULL;

int src_stdout = -1; // 一般 stdout 的 handel 是 1. stderr 是 2
int src_stderr = -1; // 一般 stdout 的 handel 是 1. stderr 是 2

void dupOutput(int fd)
{
    src_stdout = dup(STDOUT_FILENO);   // 保持老的 handle,用于恢复
    src_stderr = dup(STDERR_FILENO);
    fsync(STDOUT_FILENO);
    fsync(STDERR_FILENO);
    dup2(fd, STDOUT_FILENO);  // 把 STDOUT_FILENO 指向 fd 句柄
    dup2(fd, STDERR_FILENO);
}

void restoreOutput(int fd)
{
    fsync(STDOUT_FILENO);
    fsync(STDERR_FILENO);
    dup2(src_stdout, STDOUT_FILENO);  // STDOUT_FILENO 指向原来的 handle
    dup2(src_stderr, STDERR_FILENO);
    close(src_stdout);
    close(src_stderr);
}

int log_set_path(const char *path)
{
    if (access(path, F_OK | W_OK) == 0) {
        logHandel = fopen(path, "a+");
    }
    if (!logHandel) {
        printf("open log file(%s) error: %s\n", path, strerror(errno));
        return errno;
    }
    dupOutput(fileno(logHandel));
    return 0;
}

1.2. freopen 重定向到新文件

FILE *freopen(char *filename, char *type, FILE *stream);

freopen 类似于 fopen+dup2, 同时包含打开和重定向功能

#include <stdio.h>
int main() 
{ 
	int a,b; 
	freopen("D:\\in.txt","r",stdin); //输入重定向,输入数据将从D盘根目录下的in.txt文件中读取 
	freopen("D:\\out.txt","w",stdout); //输出重定向,输出数据将保存在D盘根目录下的out.txt文件中 
	while(scanf("%d %d",&a,&b)!=EOF) 
	printf("%d\n",a+b); 
	fclose(stdin);//关闭重定向输入 
	fclose(stdout);//关闭重定向输出 
	return 0; 
}

#include <stdio.h>
#include <iostream>
int main()
{ 
	int a,b; 
	freopen("D:\\in.txt","r",stdin); //输入重定向,输入数据将从D盘根目录下的in.txt文件中读取 
	freopen("D:\\out.txt","w",stdout); //输出重定向,输出数据将保存在D盘根目录下的out.txt文件中 
	while(cin>>a>>b) 
	cout<<a+b<<endl; // 注意使用endl 
	fclose(stdin);//关闭重定向输入
	fclose(stdout);//关闭重定向输出 
	return 0; 
}

1.3. 命令行重定向

这里主要是重定向符号的使用

新建文件 > 追加文件 >>
nohup java -jar app.jar >log 2>&1 &
简写 nohup java -jar app.jar &>log &

1.4. 参考资料

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值