Solved

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

Posted on 2003-12-05
9
257 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
My eclipse editor won't start ? 4 154
How to set environment variables in C 2 84
C++ vs C compilers 13 159
Unable to start eclipse ? 17 142
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

803 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