Velocity Reviews

Velocity Reviews (
-   C Programming (
-   -   Re: getting perl and C working together in a way that makes sense (

Johann Klammer 02-01-2013 08:30 AM

Re: getting perl and C working together in a way that makes sense
Cal Dershowitz wrote:
> $ cat perl1.c
> #include <stdio.h>
> int
> main (int argc, char *argv[])
> {
> // write perl file to a string
> char text[1000];
> FILE *fp = fopen ("", "r");
> int i = 0;
> while (feof (fp))
> text[i++] = fgetc (fp);
> text[i] = '\0';
> // output perl source to file
> FILE *fp;
> char *perlfile = "/tmp/"; // edit
> // add error checking to open/flose/system
> fp = fopen (perlfile, "w");
> fprintf (fp, "%s\n", perlsource);
> fclose (fp);
> char cmd[100];
> sprintf (cmd, "perl %s %s", perlfile, argv[1]);
> system (cmd);
> }
> // gcc -Wall -Wextra perl1.c -o out
> $ gcc -Wall -Wextra perl1.c -o out
> perl1.c: In function ‘main’:
> perl1.c:17:9: error: redeclaration of ‘fp’ with no linkage

There's two variables called fp in the same scope. This is a problem.
> perl1.c:10:9: note: previous definition of ‘fp’ was here
> perl1.c:22:24: error: ‘perlsource’ undeclared (first use in this function)

There is no variable called perlsource, but you are using one on line
22. This is a problem.
> perl1.c:22:24: note: each undeclared identifier is reported only once
> for each function it appears in
> perl1.c:27:3: warning: implicit declaration of function ‘system’
> [-Wimplicit-function-declaration]

You forgot to include some header. Could be a problem. Either 'man 3
system' or 'man 2 system' will tell you that it is stdlib.h that's missing.
> perl1.c:9:8: warning: variable ‘text’ set but not used
> [-Wunused-but-set-variable]

You set a variable to some value but did never use it..(something is
wrong here)
> perl1.c:5:11: warning: unused parameter ‘argc’ [-Wunused-parameter]

He's just teasing you...
> perl1.c:28:1: warning: control reaches end of non-void function
> [-Wreturn-type]
> $

Polite people are expected to return an int from main().
> This is from a post in c.l.p.misc, and I thought I would play with it a
> bit, because I've got something similar up my sleeve.
> I think the intent of this program speaks for itself, and it cuts across
> some of the greatest standing arguments in the C world: how do you
> declare and handle the input of a string whose size you don't know?

2)malloc(count)+read/fread(or fail)
> I've taken all the guess work out of that:
> $ ls -l perl1.c
> -rw-rw-r-- 1 fred fred 578 Feb 1 00:03 perl1.c
> $
> This is one of these days where everything I've tried has failed in a
> way I don't understand, so I'd like to ask for some help straightening
> out my syntaxes.

You can get the size of the file by doing some fseek with 0,SEEK_END and
ftell afterwards. The loop has to be done differently. Either use the
size as abort condition or check the fgetc return value.
> Where I'm going with this past this interesting riddle is that I want to
> pass data to fortran using the ISO C bindings, and the above method is
> way to primitive for what I have in mind.

?! are they not binary compatible? Writing C header files should do...
> That perl and C can communicate with each other as much as they need to
> is I think what drove the development of both languages. And none of it
> stops when Dennis Ritchie dies.
> Anyways, how do I fix this program?

What do you want to do, exactly?

All times are GMT. The time now is 03:49 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.