PDA

View Full Version : Can i copy the output of system() to an array?



xenanovich
05-25-2010, 03:35 AM
Hi,
is it possible to write the output of a System() command to an array instead of the standard output?
i am trying to compute the cksum values of individual lines in a text file and store them into an array.
what is the best way to do this?
i tried running a loop to do the following:
1. reading each line into a file,
2. computing the checksum of that file,
3. writing the cksum of this to another file
4. and reading out of this file.

this seems to be a rather cumbersome method, also, it doesn't work.
what is the best way to do this?

thanks!

np2k
05-25-2010, 05:17 AM
I've understand only the first part of your message:


is it possible to write the output of a System() command to an array instead of the standard output?

you can use a pipe&dup2 function to redirect the output of system() to the read-end of the pipe...If this does not say anything, let me know that I'll be more clear

xenanovich
05-25-2010, 05:23 AM
thanks for replying.
i've only just started coding in linux, and i'd be grateful if you could elaborate a little more...
thanks.

tabstop
05-25-2010, 07:09 AM
I think your four-step process given would be about 95 times simpler and easier for you to do, assuming you yourself have the code for computing a checksum and aren't farming it out to another program. If you are farming it out, then you're pretty much stuck with cumbersome.

By default all you get is the return value of a system() call, not what was printed out to the screen. If you need that output, you'll have to set up a pipe (type "man pipe" and "man dup2" on your system for the manual).

np2k
05-25-2010, 07:51 AM
thanks for replying.
i've only just started coding in linux, and i'd be grateful if you could elaborate a little more...
thanks.

as soon as i have a bit of time i write you the solution for get the output of system

np2k
05-25-2010, 08:11 AM
for example, if you want the output of ls command you can use the following code.
but...first of all: this is a dirty solution: if your program must does other things, you will fork the process and in the child use execl.
then: in my solution the output (for semplicity) is sent to a file (argv[1]) but you can throw it wherever you want once you put it in a file.



#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>

int main(int argc, const char* argv[]) {
int fd = open(argv[1],O_CREAT|O_RDWR,S_IRUSR|S_IWUSR);
dup2(fd,STDOUT_FILENO);
execlp("ls","ls",0);
return 0;
}

Salem
05-25-2010, 09:38 AM
popen() is a simple way of running a command and reading (or writing) to it using the standard library calls (fgets etc).

You even get a FILE* as your handle to the I/O of the program you're running.


All the fork/exec/pipe/dup stuff is hidden from you.

MK27
05-25-2010, 10:32 AM
Ditto on the value of popen(). The only issue (altho it is the same issue with pipes) is that it redirects stdout, and command output can potentially be to stderr. To get around that, redirect stderr in the command string, eg:

ls -d 2>&1

np2k
05-25-2010, 11:39 AM
popen() is a simple way of running a command and reading (or writing) to it using the standard library calls (fgets etc).

yes, it's an elegant solution

xenanovich
05-26-2010, 12:25 AM
thanks salem, np2k, mk27 !
i used popen. works like a dream! :)
@tabstop, i didn't write the code for checksum. i was using the unix cksum for this computation.