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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Integrating Unix and C++

Status
Not open for further replies.

DigitalPython

Programmer
Apr 4, 2000
2
US
I use Red Hat Linux and I use the gcc/c++ compiler that came with it. When I want to write a program that uses the system, I use the syscall.h header file.<br><br>For example, to see how many lines in a file:<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color=red><FONT FACE=monospace>system(&quot;wc -l /etc/passwd&quot;);</font></font><br><br>The output to the screen would be a number. <b>How would I set that number to a integer value in C++?</b><br>I have tried things like:<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color=red><FONT FACE=monospace>int a;</font></font><br>&nbsp;&nbsp;&nbsp;&nbsp;<font color=red><FONT FACE=monospace>a=system(&quot;wc -l /etc/passwd&quot;); </font></font><br><br>but that doesn't help at all. I know that that calls a system command, but there should be a way to set a system to response to a variable. Please tell me how to do this or supply a little code that shows how its done. THANKS.<br>
 
Hi DigitalPython,

The function [tt]popen[/tt] will do what you want.

It lets you run an external command and read that commands output as if it were a file.
Mike
michael.j.lacey@ntlworld.com
 
a=system(...)

would make a = to the exit status of the system call ...

if it was ok, it would return 1 for instance ...

popen, is short for 'pipe open' allowing you to get the output from a command into C++ ...
(not Stealing MikeLaceys thunder really just explaining)
 
So what would the popen syntax look like?

dejoe
 
From the solaris man page for popen

The following program will print on the standard output (see stdio(3S)) the names of files in the current directory with a .c suffix.
[tt]
main(){
[tab]char *cmd = &quot;/usr/bin/ls *.c&quot;;
[tab]char buf[BUFSIZ];
[tab]FILE *ptr;
[tab]if ((ptr = popen(cmd, &quot;r&quot;)) != NULL)
[tab][tab]while (fgets(buf, BUFSIZ, ptr) != NULL)
[tab][tab][tab](void) printf(&quot;%s&quot;, buf);
[tab]return 0;
}
[/tt] Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top