C:如何将stderr从系统命令重定向到stdout或文件? [英] C: how to redirect stderr from System-command to stdout or file?

查看:167
本文介绍了C:如何将stderr从系统命令重定向到stdout或文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

shell命令$ avrdude -c usbtiny将文本输出到stderr.我无法用诸如无头多cos之类的命令来阅读它,它不是标准输出.我希望文本输出到文件或文件.我该如何在C语言中完成?我已经尝试通过我的最后一个问题解决问题,但仍未解决.

The shell command $ avrdude -c usbtiny outputs text to stderr. I cannot read it with commmands such as head-less-more cos it is not stdout. I want the text to stdout or to a file. How can I do it in C? I have tried to solve the problem by my last question but still unsolved.

推荐答案

我没有在OpenBSD中尝试过类似的方法,但是在至少一些* nix类系统中,可以使用dup2来实现. /p>

I've not tried something like this in OpenBSD, but in at least a few *nix-like systems, you can do this using dup2.

#include <unistd.h>
#include <stdio.h>

int main(void) {  

  fprintf(stderr, "This goes to stderr\n");

  dup2(1, 2);  //redirects stderr to stdout below this line.

  fprintf(stderr, "This goes to stdout\n");
}

这篇关于C:如何将stderr从系统命令重定向到stdout或文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆