Re: getting perl and C working together in a way that makes sense
Cal Dershowitz <email@example.com> writes:
> $ cat perl1.c
> #include <stdio.h>
> main (int argc, char *argv)
> // write perl file to a string
> char text;
You're assuming that the file is no bigger than 1000 bytes.
> FILE *fp = fopen ("capture3.pl", "r");
> int i = 0;
> while (feof (fp))
> text[i++] = fgetc (fp);
This loop won't read anything, since feof(fp) is initially false.
Perhaps you meant `while (!feof (fp))`, but that's also incorrect. An
input loop should be terminated based on the result returned by the
input function (fgetc in this case), not on a call to feof(). See the
FAQ's section on stdio, http://www.c-faq.com/.
> text[i] = '\0';
> // output perl source to file
> FILE *fp;
> char *perlfile = "/tmp/doppelgaenger.pl"; // edit
> // add error checking to open/flose/system
> fp = fopen (perlfile, "w");
> fprintf (fp, "%s\n", perlsource);
> fclose (fp);
You terminate the input buffer with a null character so you can
treat it as a string, then use fprintf to print it. It would be
more straightforward to keep track of how many bytes you've read,
and then use fwrite() to write it.
> char cmd;
> sprintf (cmd, "perl %s %s", perlfile, argv);
> system (cmd);
It's not clear what the point of the whole program is. As written,
it copies "capture3.pl" to "/tmp/doppelgaenger.pl" and executes
it as a Perl script. Why not just execute "capture3.pl" in place?
Do you intend to extend the program so it takes input from something
other than a file, or modifies the Perl code before writing it?
There's nothing wrong with starting with the simplest case and getting
that working before adding bells and whistles. I just can't tell where
you're going with this.
Keith Thompson (The_Other_Keith) firstname.lastname@example.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
|All times are GMT. The time now is 10:10 PM.|
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.