LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1200|回复: 12

急求 ->如何重定向stdout到一个char指针?

[复制链接]
发表于 2003-12-8 22:50:05 | 显示全部楼层 |阅读模式
有内存文件吗? 谢谢 ;)
发表于 2003-12-8 23:06:03 | 显示全部楼层
open(STDOUT,char);
open(RE,>&STDOUT);
好象是这样。
 楼主| 发表于 2003-12-9 00:18:22 | 显示全部楼层

我不太懂,我用的是c

有没有重定向的办法?
 楼主| 发表于 2003-12-9 18:24:44 | 显示全部楼层

帮忙呀

帮忙呀
发表于 2003-12-9 18:34:41 | 显示全部楼层
---------------------------------
如何重定向stdout到一个char指针
---------------------------------

i am confused,what do u mean?
发表于 2003-12-9 18:39:36 | 显示全部楼层
用fprintf吧
print到char数组中就可以了
不过,没有它也没关系吧?
 楼主| 发表于 2003-12-9 19:29:53 | 显示全部楼层

是这样的

我用libpq 编程读取 postgres数据库, 结果会输出倒stdout, 我想用字符变量存储结果.  即截获stdout, 并重定向输出到我的字符串变量中, 如何做呢? 困扰我好多天了..
e.x.   fprintf(stdout,"dsfsadfasf");
         如何使我的char *string;  能够得到stdout流的内容.   或者我的stdin, fscanf(stdin,"%s",&string); 能够截获其他的stdout输出?
谢谢, 急求......
发表于 2003-12-9 19:58:14 | 显示全部楼层
应该用管道吧
发表于 2003-12-9 20:22:14 | 显示全部楼层
Use a pipe.
the postgres's stdout redirect to the pipe in.
and you can create a buffer to read from the pipe out .
good lucky!
发表于 2003-12-9 21:53:10 | 显示全部楼层
use "dup2" to redirect the STDOUT_FILENO to other file,
then read the information from file to string

example:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>

void err_exit(const char * str)
{
  fprintf(stderr,"%s\n",str);
  exit(1);
}

int main(int argc,char ** argv)
{
  int fd;
  char buf[]="zxcvbnm";
  char zzz[16];

  if((fd=open("./1.txt",O_RDWR|O_CREAT))<0)
    err_exit("error to open file!");
  
  dup2(fd,STDOUT_FILENO);

  if(write(STDOUT_FILENO,buf,sizeof(buf))<0)
    err_exit("error to open file!");

  lseek(STDOUT_FILENO,0,SEEK_SET);

  if(read(STDOUT_FILENO,zzz,sizeof(buf))<0)
    err_exit("error to open file!");

  /*did i get it?*/
  fprintf(stderr,"%s\n",zzz);

  return 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表