Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

files and pipes and streams, oh my! 1

Status
Not open for further replies.

mbaranski

Programmer
Nov 3, 2000
421
US
I'm trying to do 3 things:

1. Open a pipe to &quot;mysql -fs&quot; and write to it using << operators:

I can't figure out how to use popen() and stuff to get an ostream out of that process.
i.e.
FILE *mysql;
mysql = popen(&quot;mysql -fs&quot;, &quot;w&quot;);
/* Where to go now? */

2. Open a file &quot;carma.log&quot; to write to using <<. How do I do this?
FILE *logfile
logfile = fopen(&quot;carma.log&quot;, &quot;w&quot;);
/* Now what?*/

3. Re-direct stdout (I've got this down...)

any suggestions?
Thanks
MWB.

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
MWB,

I don't have current access to a UNIX box at the moment so I can only say that you need to create objects from the 'stream' library to perform '<< and >>' style IO.

There are old 'standard stream' libraries but the most current stream objects are in the STL.

Good luck
-pete
 
yeah, I just can't figure how to redirect a pipe opened, as in example 1, and then use the stream ops. Also, in example 2...

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
mbaranski, have you figured anything out with pipes? I found pfstream.h is useful, however in my case, the author of a program i want to pipe from uses stderr instead of stderr. Have you found any other ways to pipe, I'm looking for one. Thanks
 
Here is a sample to work with a pipe. This sample looks, if a program is already running and returns the number of running programs.
But you can write as well to a pipe like to a file.

int send_emsgs_already_running ()
{
FILE *fps;
const char *ps = &quot;ps -e|grep iaksend_emsgs&quot;;
char pspid[10], pstty[15], pstime[10], pscmd[300];
int rc = 0;
int frc = 0;

fps = popen (ps, &quot;r&quot;);
if (fps == NULL)
{
Errorhandling...
return -1;
}
while (frc != EOF)
{
strcpy ((char *)pscmd, &quot;&quot;);
frc = fscanf (fps, &quot;%s %s %s %s&quot;, pspid, pstty, pstime, pscmd);
if (strlen (pscmd))
{
// printf (&quot;Found programs: %s %s %s \n&quot;, pspid, pstty, pstime, pscmd);
if (! strcmp ((char *)pscmd, &quot;iaksend_emsgs&quot;))
{
rc += 1;
}
}
}
pclose (fps);
return rc;
}



A link where you can find more information about pipes:

Hope that helps Arno
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top