unix commands in c++

B

blackbug

I am trying to write a small code which uses unix commands
i am using the system() function call to execute the commands.

but this will just execute them and would return back true or false,
what if i need to feed the output generated by executing command in
the variables of my c++ program?

example:
system("ls | wc -l");

this would return the number of output rows, for eg say number of rows
are 6.
and now i want to use this number '6' as a value to any variable in my
c++ code.

Please let me know if anyone know solution for this.
Also, I was looking for some c++/c implementation of some unix
commands, if we can find them.

Thanks

regards,
KK
 
N

Niklas Holsti

blackbug said:
I am trying to write a small code which uses unix commands
i am using the system() function call to execute the commands.

but this will just execute them and would return back true or false,
what if i need to feed the output generated by executing command in
the variables of my c++ program?

example:
system("ls | wc -l");

this would return the number of output rows, for eg say number of rows
are 6.
and now i want to use this number '6' as a value to any variable in my
c++ code.

Look at the POSIX popen() and pclose() functions. popen() is like
system() but gives you a stream from which you can read the output of
the command, as text. When you are done, you should close the stream
with pclose().
 
R

Rolf Magnus

blackbug said:
Also, I was looking for some c++/c implementation of some unix
commands, if we can find them.

A lot of those could be found on gnu.org. Search e.g. for the coreutils
package.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top