Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

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,
0
Solaron
Asked:
Solaron
  • 4
  • 4
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now