Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

convert c-code into perl-code

by koppi2342 (Initiate)
on Dec 29, 2012 at 11:38 UTC ( #1010810=perlquestion: print w/replies, xml ) Need Help??
koppi2342 has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks, can you help me, converting the following C-Code into Perl-Code?
#include <stdio.h> int main(int argc, char **argv) { if (argc == 1) { printf("usage: cm3_checksum \n"); return 1; } FILE *file; if ((file = fopen(argv[1], "r+")) == NULL) { return 1; } /* The checksum of the exception vector */ unsigned int i, n, checksum=0; for (i=0; i<7; i++) { fread(&n, 4, 1, file); checksum += n; } /* The 2's complement of the checksum */ checksum = -checksum; printf("checksum: 0x%X\n", checksum); /* write back the checksum to location 7 */ fwrite(&checksum, 4, 1, file); fclose(file); return 0; }
Kind regards, Koppi

Replies are listed 'Best First'.
Re: convert c-code into perl-code
by ww (Archbishop) on Dec 29, 2012 at 12:44 UTC
    Why convert? (homework?)
    What have you tried? (see On asking for help: we prefer to teach; not to play the role of code-a-matic)
    Have you done anything on your own? (if not, see Tutorials, starting at "Getting Started with Perl")

    We tend to be most responsive to those who try to help themselves

      An XP whore is going to do the HW in 3... 2...
Re: convert c-code into perl-code
by Old_Gray_Bear (Bishop) on Dec 29, 2012 at 19:52 UTC
    Insert the following statements at the top of your C code:
    #! /usr/local/bin/perl use strict; use warnings;
    Add +x to the file permissions.

    Execute the file, correct all of the warnings and errors, and 'Hey Presto' you have Perl code. (Now you will need to read up on the differences between the f-subroutines and their real counterparts, but I have to leave something as an exercise for the Student.)

    I Go Back to Sleep, Now.


      Excellent, but wrong section!

      ... this gem belongs to Tutorials! ;-)

      Cheers Rolf

      PS: not really a joke...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1010810]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2018-12-12 01:03 GMT
Find Nodes?
    Voting Booth?
    How many stories does it take before you've heard them all?

    Results (57 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!