tags:

views:

241

answers:

3

hello.

i trying make a custom method what causes return a char with system output.

the pseudocode like this.

char *my_Out(char *in ){
    in = system ("ping %s",in);
    return in;
}

thanks for the help.

+5  A: 

You can use popen, which returns you a stream that you can read the output from. By reading until end-of-file, into a string (probably one that dynamically grows as necessary), you can implement what you're asking for.

Chris Jester-Young
Though if you're looking for one of the pieces of information contained in an `ls -l`, say, you can probably get it a lot more easily with a library function (`stat`).
Jefromi
@Jefromi: +1 for your insight, although a small nit: `stat` is usually a system call, not a library function. :-P
Chris Jester-Young
Chris: `stat` is usually a library function *wrapping* a system call (he who lives by the nit...)
caf
@caf: These days, yes (because of different `struct stat` layouts between kernel versions, and the need to keep user code binary-compatible). Originally, no, it was a straight system call. Nonetheless, good call. +1
Chris Jester-Young
+2  A: 

A few things

  1. system() is not a printf style function. You'll need to use sprintf() to create your argument before.
  2. system()'s return value is an int, non a char
  3. It's generally not a good idea to overwrite function parameters.

What are you trying to do? It looks like all this function does is run ping (which, without the -c argument, will never finish running on linux).

jdizzle
I disagree with point 3. While it may be unusual and break your style, there is nothing wrong with it.
R Samuel Klatchko
+1  A: 

Duplicate the stdout to some other file descriptor by using dup2.After the execution of the command read all the lines from the file using that file descriptor and return it.

karthi_ms