Solved

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

Posted on 2003-12-05
9
270 Views
Last Modified: 2010-04-15
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
Comment
Question by:Solaron
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9886461
>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
 
LVL 84

Expert Comment

by:ozo
ID: 9887612
#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
 

Author Comment

by:Solaron
ID: 9892908
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
Industry Leaders: 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!

 

Author Comment

by:Solaron
ID: 9892940
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
 

Author Comment

by:Solaron
ID: 9893002
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
 
LVL 45

Accepted Solution

by:
sunnycoder earned 250 total points
ID: 9895472
>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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9895484
>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
 

Author Comment

by:Solaron
ID: 9895996
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9896002
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question