Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Trying to get an external c program to print to a file

by akrrs7 (Acolyte)
on Nov 04, 2011 at 11:52 UTC ( [id://935901]=perlquestion: print w/replies, xml ) Need Help??

akrrs7 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to get an external c program to print to a file. This is my code:
my $outputFile1 = "${a1}_${a2}_1.out"; my $outputFile2 = "${a1}_${a2}_2.out"; my $outputFile3 = "${a1}_${a2}_3.out"; #first attempt system("$PGMEXEC1", "-arg1=$arg1", "-arg2=$arg2", ">${workingDir}${ps +}$outputFile1"); #second attempt my $output2 = system("$PGMEXEC1", "-arg1=$arg1", "-arg2=$arg2" ); #third attempt my $output3 = `$PGMEXEC1 -arg1=$arg1 -arg2=$arg2`; open my $fh2, "> $outputFile2"; print $fh2 $output2; close $fh2; open my $fh3, "> $outputFile3";; print $fh3 $output3; close $fh3;
I do not get outputFile1...file is not created. I get different results in outputFile2 and 3.- neither are what I want. My intent is to get the fprintf(stdout) statements defined in my c program (PGMEXEC1) to print out to the *.out file. What am I doing wrong. Thanks.

Replies are listed 'Best First'.
Re: Trying to get an external c program to print to a file
by Eliya (Vicar) on Nov 04, 2011 at 12:42 UTC

    If you want to circumvent the shell you can fork/exec yourself (at least on Unix), and reopen STDOUT in the child process to the file you want1:

    sub my_sys { my $out = pop; my $pid = fork(); die "couldn't fork" unless defined $pid; if ($pid) { wait; } else { close STDOUT; open STDOUT, ">", $out or die "open '$out' failed: $!"; exec @_; die "exec failed: $!"; } } my_sys("/bin/echo", "foo", "bar", "std.out");
    $ ./935901.pl $ cat std.out foo bar

    1 Note that this doesn't work for Perl's "memory" filehandles, because they don't have an associated system file descriptor. Also note that fileno(STDOUT) must be 1, because that's the file descriptor your C program is going to send its stdout to.

Re: Trying to get an external c program to print to a file
by bart (Canon) on Nov 04, 2011 at 11:54 UTC
    You can't redirect STDOUT of an external program with system without using the shell. So you should be using a command line as the single argument to system. Multiple arguments just won't work in this case.
      Hello Bart, I did not follow your suggestion. Thanks...
        I mean that
        system("$PGMEXEC1", "-arg1=$arg1", "-arg2=$arg2", ">${workingDir}${ps +}$outputFile1");
        will not work, you have to use something like
        system("$PGMEXEC1 -arg1=$arg1 -arg2=$arg2 >${workingDir}${ps}$outputFi +le1");
        See system for the difference between the two.

        If your variable arguments can contain spaces or other characters that are special for the shell, you'll have to escape them. If on Linux or another Unix-derivative, you can use the module String::ShellQuote to handle the hard work. If on Windows, just putting double quotes around the arguments should do, unless you're making life really hard on yourself. (You can wrap the whole command line in qq so you don't have to escape the quotes. Oh, you can't use single quotes there because the variables need to be replaced by their values.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://935901]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-03-29 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found