Re: getting perl and C working together in a way that makes sense

Discussion in 'C Programming' started by Keith Thompson, Feb 1, 2013.

  1. Cal Dershowitz <> writes:
    > $ cat perl1.c
    >
    > #include <stdio.h>
    >
    > int
    > main (int argc, char *argv[])
    > {
    >
    > // write perl file to a string
    > char text[1000];


    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 = '\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[100];
    > sprintf (cmd, "perl %s %s", perlfile, argv[1]);
    > 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) <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"
    Keith Thompson, Feb 1, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johann Klammer
    Replies:
    0
    Views:
    206
    Johann Klammer
    Feb 1, 2013
  2. Malcolm McLean
    Replies:
    5
    Views:
    222
    Bart van Ingen Schenau
    Feb 2, 2013
  3. Mark Bluemel
    Replies:
    2
    Views:
    206
    James Kuyper
    Feb 1, 2013
  4. Jens Schweikhardt
    Replies:
    24
    Views:
    461
    Jorgen Grahn
    Feb 4, 2013
  5. Jorgen Grahn
    Replies:
    0
    Views:
    214
    Jorgen Grahn
    Feb 2, 2013
Loading...

Share This Page