将stdout/stderr重定向到C语言中的功能 [英] redirect stdout/stderr to function in C

查看:79
本文介绍了将stdout/stderr重定向到C语言中的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将stdout和stderr重定向到一个函数并编写一个简单的代码.fprintf可以,但是当我使用不带\ r的printf时,流传递到终端!

  #include< stdio.h>静态ssize_t file_write(void * c,const char * buf,size_t大小){FILE * pFile;pFile = fopen("output.txt","w");fprintf(pFile,%s",buf);fclose(pFile);返回大小;}无效redirect(){FILE * stream;stream = fopencookie(NULL,"w",(cookie_io_functions_t){(ssize_t)0,file_write,0,0});setbuf(stdout,NULL);stdout =流setbuf(stderr,NULL);stderr =流;}int main(){redirect();fprintf(stderr,"1-stderr test \ n");fprintf(stdout,"2-stdout测试\ n");printf("3-printf with r test \ n \ r");printf("4-printf,不进行r测试\ n");返回0;} 

文件"output.txt":

1-stderr测试
2-stdout测试
带r测试的3-printf

端子输出:

$ ./sample
不带r检验的4-printf

解决方案

您不能从程序中的函数重定向"标准输入或将标准输出重定向"到该函数.

但是,您可以创建一对管道,并用这些管道替换标准的输入和输出,并使另一个功能(可能在另一个线程甚至是子进程中)使用管道的另一端与该功能进行通信./p>

您当然可以使用例如 setvbuf 更改 stdin的缓冲区 stdout 到您提供的一对缓冲区,然后您就可以像其他任何内存缓冲区(即数组)一样直接从缓冲区读取或直接写入缓冲区.并不是我建议这样做,因为它很脆弱并且容易出错.

但是,在这种情况下,您似乎想要的是将所有写入 stdout 的内容都写入该文件.在那种情况下(例如使用管道),没有标准的C处理方式,但是您需要使用特定于操作系统的功能.然后,如果您使用的是POSIX平台(如Linux或OSX),则可以告诉操作系统复制文件的基础文件描述符,并说 STDOUT_FILNO (用于 stdout )与您打开的文件的文件描述符重复.然后,所有对 stdout 的写操作都会被写到您的文件中.为此,您需要使用 dup2 系统调用.

I want to redirect stdout and stderr to a function and write a simple code. fprintf is ok but when i use printf without \r, stream passed to terminal!

#include <stdio.h>
static ssize_t file_write(void *c, const char *buf, size_t size)
{
  FILE * pFile;
  pFile = fopen ("output.txt","w");
  fprintf(pFile, "%s", buf);
  fclose (pFile);

return size;
}

void redirect()
{
  FILE *stream;
  stream=fopencookie(NULL, "w", (cookie_io_functions_t) {(ssize_t) 0,  file_write, 0, 0});
  setbuf(stdout, NULL);
  stdout = stream;
  setbuf(stderr, NULL);
  stderr = stream;
}

int main()
{
  redirect();
  fprintf(stderr,"1-stderr test\n");
  fprintf(stdout, "2-stdout test\n");
  printf("3-printf with r test\n\r");
  printf("4-printf without r test\n");

return 0;
}

file "output.txt":

1-stderr test
2-stdout test
3-printf with r test

Terminal output:

$ ./sample
4-printf without r test

解决方案

You can't "redirect" standard input from, or standard output to a function in your program.

You can however create a pair of pipes and replace standard input and output with these pipes, and have another function (possibly in another thread or even a child process) use the other end of the pipes to communicate with the function.

You can of course use e.g. setvbuf to change the buffers of stdin and stdout to a pair of buffers you provide, then you can read directly from, or write directly to the buffers like any other memory buffer (i.e. array). Not that I recommend it though, it's frail and prone to errors.

However in this case it seems like what you want is that all writing to stdout should go to the file. In that case (like the use of pipes) there is no standard C way of handling it, but you need to use operating system specific functionality. Then if you're on a POSIX platform (like Linux or OSX) you can tell the operating system to duplicate the underlying file descriptor for your file, and say that STDOUT_FILNO (which is the file descriptor used for stdout) is a duplicate for the file descriptor of the file you opened. Then all writes to stdout will be written to your file. For this you need to use the dup2 system call.

这篇关于将stdout/stderr重定向到C语言中的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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