Thread: Calling SAC commands from C program

Started: 2013-10-11 04:39:38
Last activity: 2013-10-11 16:46:44
Topics: SAC Help
Mohit Agrawal
2013-10-11 04:39:38
Hello SAC users,

I want to add a line something like this in a C program

*sprintf(str,"cd rdtmp; sac; r *.SAC; interpolate delta 1.0; w over; quit;
../%s");
system(str);*

What I want to do is that C program opens SAC and read in the files with
extension ".SAC" and then interpolate the data. But when I am adding the
above lines, It always stuck just after opening SAC and don't run the
following commands.

Any suggestion in this regard will be appreciated.

Thanks,

-Mohit

  • Bob Dunlop
    2013-10-11 16:46:44
    Hi,

    On Thu, Oct 10 at 09:39, mohit agrawal wrote:
    Hello SAC users,

    I want to add a line something like this in a C program

    *sprintf(str,"cd rdtmp; sac; r *.SAC; interpolate delta 1.0; w over; quit;
    ../%s");
    system(str);*

    This is really about C programming and command execution environment
    rather than SAC. You might find more help on a general linux programming
    list, but here goes.

    The SAC commands need to be presented to the SAC program on it's standard
    input, not as separate commands to be executed by the system shell. You
    need to use popen() rather than system() so as to give you access to the
    standard input pipline. Try this:

    FILE *fp;

    /* Start the SAC process with write access to stdin */
    fp = popen( "cd rdtmp; sac", "we" );

    /* Send the SAC commands to the process */
    fprintf( fp, "%s", "r *.SAC\ninterpolate delta 1.0\nw over\nquit\n" );

    /* Cloes the pipe and wait for SAC process to complete */
    pclose( fp );

    This example is untested, has no error checking and assumes that you want
    the output from the SAC command to go to the current standard output, but
    I hope you get the idea.

    If you want to capture the output as well things get more complex. With
    full error checking, handling stdin and stdout and all the potential locking
    issues that implies it about 150 lines of code to do it right.

    Any suggestion in this regard will be appreciated.
    HTH

    --
    Bob Dunlop
    Guralp Systems Limited
    http://www.guralp.com


Page built 19:49:06 | v.b'59d0d2