printing and opening a pipe
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > printing and opening a pipe




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    printing and opening a pipe  
Billy N. Patton


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-19-04 01:48 AM

I'm calling a library function that prsnts something to stdout.

Long story but I can't change this function for now, too much ripple.

I need to
1. open a pipe to capture stdout.
2. save to string.
3. return string
4. close pipe.


Ony thing I have is:

bool ExCommand(string& cmd,vector<string>& ret)
{
string s;
static  char      buf[BUFSZ];
FILE    *ptr      = NULL;

if (cmd.empty()) return false;

if ((ptr = popen(cmd.c_str(), "r")) NE NULL)
{
while (fgets(buf, BUFSZ, ptr) NE NULL)
{
buf[strlen(buf)-1] = '\0';
s = buf;
ret.push_back(s);
}
pclose(ptr);
}
else
{
return false;
}
return true;
}


But this executes an extern shell command.  I'm calling a function
inside a library.



--
___  _ ____       ___       __  __
/ _ )(_) / /_ __  / _ \___ _/ /_/ /____  ___
/ _  / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/   \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodlogy Group
Dallas, Texas, 214-480-4455,  b-patton@ti.com





[ Post a follow-up to this message ]



    Re: printing and opening a pipe  
Lew Pitcher


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-19-04 01:48 AM

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Billy N. Patton wrote:
> I'm calling a library function that prsnts something to stdout.
>
> Long story but I can't change this function for now, too much ripple.
>
> I need to
> 1. open a pipe to capture stdout.
> 2. save to string.
> 3. return string
> 4. close pipe.
[snip]

That seems a bit difficult to do with pipes.

Have you considered an alternative, like using freopen(3) to capture stdout 
to a
file, then reading the file?


- --
Lew Pitcher
IT Consultant, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

 iD8DBQFBdAkIagVFX4UWr64RAvW5AKCSEnhAc7ed
47uWucul8RhteB+nlwCfVsTK
fNJVXzwNbB/OD8p5M8vL3S4=
=54+A
-----END PGP SIGNATURE-----





[ Post a follow-up to this message ]



    Re: printing and opening a pipe  
Billy N. Patton


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-19-04 01:48 AM

Lew Pitcher wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Billy N. Patton wrote:
> 
>
> [snip]
>
> That seems a bit difficult to do with pipes.
>
> Have you considered an alternative, like using freopen(3) to capture stdou
t to a
> file, then reading the file?

Yes but don't want to do that much IO.
Besides I may have 100's running in batch around the network.
Bookkeeping could get nasty for unique file names.

>
>
> - --
> Lew Pitcher
> IT Consultant, Enterprise Data Systems,
> Enterprise Technology Solutions, TD Bank Financial Group
>
> (Opinions expressed are my own, not my employers')
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.4 (MingW32)
>
>  iD8DBQFBdAkIagVFX4UWr64RAvW5AKCSEnhAc7ed
47uWucul8RhteB+nlwCfVsTK
> fNJVXzwNbB/OD8p5M8vL3S4=
> =54+A
> -----END PGP SIGNATURE-----


--
___  _ ____       ___       __  __
/ _ )(_) / /_ __  / _ \___ _/ /_/ /____  ___
/ _  / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/   \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodlogy Group
Dallas, Texas, 214-480-4455,  b-patton@ti.com





[ Post a follow-up to this message ]



    Re: printing and opening a pipe  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-19-04 01:48 AM

In article <cl101j$h4g$1@home.itg.ti.com>,
"Billy N. Patton" <b-patton@ti.com> wrote:

> I'm calling a library function that prsnts something to stdout.
>
> Long story but I can't change this function for now, too much ripple.
>
> I need to
> 1. open a pipe to capture stdout.
> 2. save to string.
> 3. return string
> 4. close pipe.
...

> But this executes an extern shell command.  I'm calling a function
> inside a library.

What you could do is something like the following pseudocode:

dup(saved_stdout, STDOUT_FILENO);
pipe(pipefd);
dup2(STDOUT_FILENO, pipefd[1]);
close(pipefd[1]);
create a thread that reads from pipefd[0] and accumulates it into a
string
call the function
dup2(STDOUT_FILENO, saved_stdout);
The thread should now exit when it reads EOF from the pipe
close(pipefd[0])
Process the string

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:33 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register