C++: cin from a shell command?

lopoetve

Extremely [H]
2FA
Joined
Oct 11, 2001
Messages
34,262
Ok, running a c++ program, and I want to run a shell command (system(cat /proc/cpuinfo | grep processor | wc -l))....

How would I read the result (it outputs a single number to stdout) into my program without using a filebuffer, and how would I need to change that command to make it work?

Thanks,
Lopoetve
 
Which operating system?

The portable way to do it is to invoke your comamnd, redirecting the output to a temporary file. When the process ends, you go read the file.

If you want to do it on the fly without a temp file you'll need to rely on an OS-specific interprocess communication mechanism. So we'll need to know which OS you're targeting.
 
look into the popen() commands if you're running unix. THey might only work with the older C-style input commands tho...
 
mikeblas said:
Which operating system?

The portable way to do it is to invoke your comamnd, redirecting the output to a temporary file. When the process ends, you go read the file.

If you want to do it on the fly without a temp file you'll need to rely on an OS-specific interprocess communication mechanism. So we'll need to know which OS you're targeting.

Linux, either a RedHat version or SuSE. We want to do it without the file if we can, although we have that working if we have to.
 
ameoba said:
look into the popen() commands if you're running unix. THey might only work with the older C-style input commands tho...

Pipes?
 
if its written to stout cin should be able to read it. lemme play around with an example for a minute
edit: yes this in fact works
edit: wait a second maybe not


Edit: on second thought, you should rethink the way you are structuring this program. Is there any good reason you couldn't just pipe the output from that command into your program from the very beginning? Doing that, and simply writing a bash script to do it every time would have been completed in probably less time than it took for you to post this on the hard forums.

lopoetve said:
Linux, either a RedHat version or SuSE. We want to do it without the file if we can, although we have that working if we have to.

This can demonstrate my point. CPU time is cheap, employee work time is not. Using the spare file or using my example you'd be finished and done with, probably saving more time and definately more money than by not using a temporary file. Work smart, not hard :D
 
pipes are |, >, >>, etc.

They are used to redirect the output of one command into another. Example is "more somefiles | grep searchstring"

that will redirect the output of more, instead of to the screen, to the input of grep. more outputs the contents of a text file, grep searching line by line of its input for a search string, and outputs lines where thats found. Piping more into grep will search the file specified and will display only lines where the searchstring is found.

Welcome to the unix way of doing things.

lopoetve said:
 
Whatsisname said:
if its written to stout cin should be able to read it. lemme play around with an example for a minute
edit: yes this in fact works
edit: wait a second maybe not


Edit: on second thought, you should rethink the way you are structuring this program. Is there any good reason you couldn't just pipe the output from that command into your program from the very beginning? Doing that, and simply writing a bash script to do it every time would have been completed in probably less time than it took for you to post this on the hard forums.



This can demonstrate my point. CPU time is cheap, employee work time is not. Using the spare file or using my example you'd be finished and done with, probably saving more time and definately more money than by not using a temporary file. Work smart, not hard :D

The scientists want it elegant. Not my fault the government pays them :p

edit: I'll suggest the script, didn't think about it ;) We wanted it built in so it was a single file, portable item though that didn't require anything but the binary to run. we'll see what they say to 2 files.
 
Whatsisname said:
pipes are |, >, >>, etc.

They are used to redirect the output of one command into another. Example is "more somefiles | grep searchstring"

that will redirect the output of more, instead of to the screen, to the input of grep. more outputs the contents of a text file, grep searching line by line of its input for a search string, and outputs lines where thats found. Piping more into grep will search the file specified and will display only lines where the searchstring is found.

Welcome to the unix way of doing things.

I know what a pipe is, I'm a sysadmin at a massive research firm. I was asking if popen was for pipes ;) I've never used popen before, so... :p

lol. :D

I have more HD space than all of [H], I'd wager... ;), all running unix in some form (AIX, Irix, and SLES9 mostly)
 
i don't know that :D

and if you are to include HD space that you have access to at your job I'm sure I could argue against you :p
lopoetve said:
I know what a pipe is, I'm a sysadmin at a massive research firm. I was asking if popen was for pipes ;) I've never used popen before, so...

lol. :D

I have more HD space than all of [H], I'd wager... , all running unix in some form (AIX, Irix, and SLES9 mostly)
 
lol elagance :D Leaves so much room for interpretation. I wonder if whatever you are doing couldn't be accomplished entirely with a bash script and standard GNU/Linux commands.

lopoetve said:
The scientists want it elegant. Not my fault the government pays them :p

edit: I'll suggest the script, didn't think about it ;) We wanted it built in so it was a single file, portable item though that didn't require anything but the binary to run. we'll see what they say to 2 files.
 
Whatsisname said:
lol elagance Leaves so much room for interpretation. I wonder if whatever you are doing couldn't be accomplished entirely with a bash script and standard GNU/Linux commands.

nope. It's a radar / METAR / TAF algorithm program.

Whatsisname said:
i don't know that

and if you are to include HD space that you have access to at your job I'm sure I could argue against you :p
http://www.cisl.ucar.edu/hss/ssg/index-new.shtml

Look up staff. I'm in there. ;)

As for space... we have the largest data store in the world. Currently at 6 petabytes, growing at 60 terabytes a month, give or take a bit. :) I LOVE my job.
 
Is that the actual command you want to run? If so, it will be far easier to simply code it in C rather than using shell commands.

If the shell line can't be programmed in C for some reason, look into something like a FIFO or socket. Put one in /tmp, send output to it from the shell, and read() back from it in your program.
 
popen() gives you a file descriptor that you can read from and write to for an arbitrary command. As far as the OS is concerned, it's very similar to using pipes...
 
Lews_Therin said:
Is that the actual command you want to run? If so, it will be far easier to simply code it in C rather than using shell commands.

If the shell line can't be programmed in C for some reason, look into something like a FIFO or socket. Put one in /tmp, send output to it from the shell, and read() back from it in your program.

Code grep, wc, and cat into c? No thanks ;) Way too much extra code. We're just hoping to easily multi-thread this thing, as it's quite adaptable for that, but we need a nice easy way to determine the number of processors (And that seems to be the easiest).
 
ameoba said:
popen() gives you a file descriptor that you can read from and write to for an arbitrary command. As far as the OS is concerned, it's very similar to using pipes...

I'll look this up, thanks :)
 
Back
Top