如何重定向printf输出回代码? [英] How to redirect printf output back into code?

查看:163
本文介绍了如何重定向printf输出回代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个小c ++应用程序来包装opencv haar训练函数(即cvCreateTreeCascadeClassifier)。该函数向控制台输出一整个输出,我希望解析这个输出,以便我可以在我的代码中填充各种变量。

i'm writing a little c++ app to wrap around the opencv haar training function (namely cvCreateTreeCascadeClassifier). The function throws a whole load of output to the console and I wish to parse this output so that I can populate various variables in my code.

我希望使用的函数不是实际openCV库的一部分;而是必须使用我的代码作为项目的一部分来构建。该函数的所有输出都是通过printf。

The function I wish to use is not part of the actual openCV library; instead it has to be built with my code as part of the project. All of the output from the the function is via printf.

问题:是否可以在控制台上截断printf语句?我设法使用freopen重定向他们,但这似乎有点笨拙,因为我需要解析文件,然后在函数调用完成时删除它。此外,该函数可能运行了几个小时(甚至可能是几个星期!),因此,如果文件的大小不断被附加,则文件的大小可能会成为问题。

Question: Is it possible to intercept the printf statements before they end up on the console? I've managed to redirect them using freopen but this seems a little clumsy as I then need to parse the file and then delete it when the function call is finished. Also, the function is likely to be running for several hours (and possibly even weeks!) so the size of the file might be an issue if its constantly being appended too.

需求:我需要这个应用程序是c + +和运行在Windows和linux(但是如果需要的条件编译语句没有问题)。我也想在控制台上仍然看到我的cout和cerr消息(只是没有printf)。

Requirements: I need this app to be c++ and to run on both windows and linux (but have no problem with conditional compile statements if need be). I would also like to be able to still see my cout and cerr messages on the console (just not the printf).

我的Google搜索已经删除了我的意志!

My googling has removed my will to live! Can anyone help with a solution via either code example or pointers to places I should be looking for an answer?

感谢

推荐答案

您可以做什么:


  • 创建管道

  • 使管道的可写末端成为新的stdout

  • 从管道的可读部分读取

阅读和写作应该发生在不同的线程,否则你的程序会在管道的一端发生饥饿。

Reading and writing should happen in different threads or you risk that your program starves on one end of the pipe.

这里有一个例子在unix& Windows:

Here's a sample how to do the redirection in unix & windows:

#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
/* gcc defined unix */
#ifdef unix
#include <unistd.h>
#endif
#ifdef WIN32
#include <io.h>
#define pipe(X) _pipe(X,4096,O_BINARY)     
#define fileno _fileno
#define dup2 _dup2
#define read _read

#endif
#include <assert.h>

int main()
{
    int fds[2]; 
    int res; 
    char buf[256];
    int so; 

    res=pipe(fds);
    assert(res==0); 

    so=fileno(stdout);
    // close stdout handle and make the writable part of fds the new stdout.
    res=dup2(fds[1],so);
    assert(res!=-1); 

    printf("Hi there\n");
    fflush(stdout);
    // reading should happen in a different thread

    res=read(fds[0],buf,sizeof(buf)-1);
    assert(res>=0 && res<sizeof(buf));
    buf[res]=0;
    fprintf(stderr,"buf=>%s\n",buf);
    return 0;
}






/ p>


This code should print

buf=>Hi there

$ b b

(我在这里使用assert,因为我太懒了,为这个例子做真正的错误检查)

(I'm using assert here, because I am too lazy to do real error checking for this example)

这篇关于如何重定向printf输出回代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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