• C

How do i us a c program with my perl script?

Hi,

Simply - i have a perl script wrapper which invokes my c program. This c program is dedsigned to calculate positions of stars in the sky.

I am currently reading in masses of data from a file to test my program but now i want it to be run on a web server and instead of my perl script giving out the data to a data file (which is read in by the c program), i was hoping to have it completely automated.

I have seen a few examples of programs where they import data through stdin and then exports via stdout. But how do i do this?!!?

Currently each line of my file has 6 properties of the star (including position) which is read into a linked list (as the file size can vary due to an uncertain number of stars). But i dont want to use a file pointer as i mentioned, but have this automated.

Any ideas?

I would post my code but its mahoosive!!!

Many thanks,
SolaronAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
>i was hoping to have it completely automated.
>I have seen a few examples of programs where they import data through stdin and then exports via stdout. But how do i do
>this?!!?

>- i have a perl script wrapper which invokes my c program. This c program is dedsigned to calculate positions of stars in the
>sky.

>Currently each line of my file has 6 properties of the star (including position) which is read into a linked list

Given these constraints, you can do --

while ( fgets (buffer, MAX_BUF, stdin ) != NULL)
{      
      sscanf ( buffer, "%s %s %s %s %s %s\n", node.var1, node.var2, node.var3, node.var4, node.var5, node.var6 );
       //process the variables
}    

here node is node of linked list and var1-var6 are char * variables which store the data
Cheers
0
ozoCommented:
#then on the Perl side, you might invoke the C program with
use IPC::Open2;
$pid = open2(\*RDRFH, \*WTRFH, 'some', 'cmd', 'and', 'args');
printf  WTRFH,join' ',@{$_}{var1...var6},"\n" foreach @stars ;
close WTRFH;
$answer = <RDRFH>;
0
SolaronAuthor Commented:
That makes sense, but how would i initialise all these arguments? So fgets reads from stdin and stores into an array called buffer of size MAX_BUF. This then goes through until stdin doesn't have any more data. How would i initialise 'buffer'. An array? Dynamically allocate?

Cheers for your help
0
Become an IT Security Management Expert

In today’s fast-paced, digitally transformed world of business, the need to protect network data and ensure cloud privacy has never been greater. With a B.S. in Network Operations and Security, you can get the credentials it takes to become an IT security management expert.

SolaronAuthor Commented:
Or more simply, can you treat stdin & stdout as a file? Instead of the file you were pointing to, you could just write stdin?
0
SolaronAuthor Commented:
The input from std in looks like so:
1 12 42 21.66 0 63 15 4.62 14.023 0.0991
1 12 42 22.89 0 63 15 0.36 13.713 0.0891
:where the first integer is just a flag placing the data in the correct linked list

Here is my code. I realise this is hard to describe, so hopefully the code will do that for me. This is what i think it should be but i cannot test it just yet:

void GetData (Database *coredata, Database *intrinsic) {

      int test;
      char *buffer;

      buffer = xmalloc (MAXBUF * sizeof *buffer);

      coredata->data = NULL;
      intrinsic->data = NULL;
      coredata->next = coredata;

      while (fgets (buffer, MAXBUF, stdin) != NULL) {  
            
            sscanf (buffer, "%d", &test);

            switch (test) {

            case 1:
                  
                  coredata->data = NewObject (coredata->data, buffer);
                  break;

            case 2:

                  intrinsic->data = NewObject (intrinsic->data, buffer);
                  break;

            case 0:

                  sscanf (buffer, "%d %d", &i, &coredata->grid);
                  sscanf (buffer, "%d",&coredata->lum);
                  sscanf (buffer, "%d",&coredata->radius);
                  break;
            }
      }            
}

/* -------------------------- */
/* Functions - Objects      */
/* -------------------------- */

Object *NewObject (Object *first, char *source[MAXBUF]) {

      Object *data;
      int dump;

      data = xmalloc (sizeof *data);
        data->next = first;

      sscanf (source, "%d", &dump);
       sscanf (source, "%f", &data->ra_h);
      sscanf (source, "%f", &data->ra_m);
      sscanf (source, "%f", &data->ra_s);
      sscanf (source, "%d", &data->sign);
      sscanf (source, "%f", &data->dec_h);
      sscanf (source, "%f", &data->dec_m);
      sscanf (source, "%f", &data->dec_s);
      sscanf (source, "%f", &data->k);
      sscanf (source, "%f", &data->mag);

      data->ra_d = ((data->ra_m/60) + ((data->ra_s/60)/60) + data->ra_h)*15;
      data->dec_d = (data->dec_m/60) + ((data->dec_s/60)/60) + data->dec_h;

      if (data->sign == 0) { data->dec_d = data->dec_d * -1; }

      return data;
}

Cheers
0
sunnycoderCommented:
>can you treat stdin & stdout as a file? Instead of the file you were pointing to, you could just write stdin?
yes, every C program has 3 files open by default
stdin
stdout
stderr

> I realise this is hard to describe, so hopefully the code will do that for me. This is what i think it should be but i cannot test it
>just yet:
some explanation as to what is the problem/issues with the code is always helpful ... reading the code and the decoding what is on your mind is indeed difficult.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sunnycoderCommented:
>xmalloc (MAXBUF * sizeof *buffer);
is suspect .. check it


>Object *NewObject (Object *first, char *source[MAXBUF])
char * source [] means an array of char *s ... I think you meant

Object *NewObject (Object *first, char * source)
or
Object *NewObject (Object *first, char source[MAXBUF])

sscanf's look ok ... you can combine them into a single call if you like
0
SolaronAuthor Commented:
Sorry, i am new to things like stdin & stdout - only being taught c programming at university. But one last question and i think i have understood completely. stdin & stdout are already open with the c program. So i can just write (as the only data that will be in stdout is the data i want):

fscanf (stdin,"...wahatever...", linked-list)

and

fprintf (stdout, "...whatever...", linked-list);

??

Cheers - if that is so, then you have definately solved my problem!
0
sunnycoderCommented:
yes that is definitely so
to convince yourself, check out the stdio.h file ... these are defined there
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.