Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text file... then average the numbers in the text file and print the average.

I'm not entirely sure I'm doing this correctly (This is my first Perl program!) so if there are additional mistakes that anyone sees that will trip me up after I get this problem fixed, I'd more than welcome the feedback.

The error I'm getting with the command 'perl -w myperl':

The error I get with the command 'perl -d myperl':

When I try to just type 'perl myperl', I get a 0 printed to the screen and nothing is in datafile.txt.

My C Program (called 'myproc.c', when compiled is 'myprog'):

#include <stdlib.h>
#define RAND_MAX      10

// function prototypes
void initrand(void);
int randint(int max);

// main function
int main(void)
{
        int randomNumber;
        int max = 10;

        initrand(); // generate the seed
        randomNumber = randint(max); // get the random number
        return randomNumber;
}

// this function seeds the random number generator
void initrand()
{
        srand((unsigned)(time(0)));
}

// this function generates a psuedo-random integer between 0 and max
int randint(int max)
{
        return (max*rand()/(RAND_MAX+1));
}

My Perl program (called 'myperl'):

# assign file handler for myprog
$outfile = 'datafile.txt';      # name file we're printing output to
open(INFO, $outfile);           # open file for output

for ($i = 0; $i < 20; $i++)     # run myprog 20 times
{
        $randNum = system(`myprog`);    # Get data from the C program
        print INFO $randNum;            # print collected data to $outfile
        print INFO '/n';                # print newline to $outfile
}

# assign input file handler
$infile = 'datafile.txt';       # name file to open for input
open(INFO, $infile);            # open file for input
@rands = <INFO>;                # put all the data from file into an array
print @rands;                   # print the array to get the contents of file

$j = 1;         # counter for averaging
$sum = 0;       # keeps up with final sum
foreach $number (@rands)        # loop through data to get the average
{
        $sum = $sum + $number;
        $j++;
}

$average = $sum / $j;   # calculate average
print "$average\n";     # print new average to the screen

School info: Arkansas State Univerity, Jonesboro, AR, Dr. Hai Jiang, CS 4-5223

---------- Post updated 02-20-11 at 12:35 PM ---------- Previous update was 02-19-11 at 06:03 PM ----------

I actually got this figured out with the help of a friend, so here's that answer if it helps anyone:

FOR THE PERL PART
When opening files for input and output in Perl, do it like this:
output-

open(INFO, '>' . $outfile);

input-

open(INFO, '<' . $infile);

To run the C program within your perl program:

$randNum = system(`./myproc`);    # Get data from the C program

FOR THE C PART
I had a problem within my initrand function which was causing all the output numbers to be the same. This is caused by the fact that the srand() function for seeding is by the second, which doesn't work when trying to run a program multiple times per second. This is a problem I'm actually still trying to work on a little, but that's a good first step towards solving your own problem.

I was trying to REdefine RAND_MAX, so that was renamed to RANDINT_MAX.

When casting an int, use parentheses:
return (int) (/*stuff*/);

  1. RAND_MAX does not work that way. RAND_MAX is something the compiler tells you -- the highest possible number the random number generator will ever give you. Usually, to generate numbers between 0-9, you'd do (rand()%10) , and for numbers 1-10, (rand()%10)+1 .
  2. This isn't DOS, you can't just run programs that happen to be in the current directory and expect them to be found. You have to tell it it's in the current directory by telling it ./myprog , not just myprog
  3. There's no point running system() on things you put in backticks. Do one, or the other: $code=system("./myprog"); $stuff=`./myprog`;
  4. system() doesn't return the program's text, it returns the program's return code (usually zero). If you want it's output, that's what backticks are for. $stuff=`./myprogram`;
  5. Perl almost certainly has the ability to generate random numbers without calling a C program. In fact perl has a rand() too (try perldoc -f rand) which seems more convenient. You can just do $stuff=int(rand(10))+1; to get numbers 1-10. Modern versions of perl also call srand by themselves when run, so you get a different random starting point every time without any effort on your part.