Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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

Comment on convert c-code into perl-code
Download Code
Replies are listed 'Best First'.
Re: convert c-code into perl-code
by ww (Bishop) 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 browsing the Monastery: (13)
As of 2015-11-30 18:54 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (778 votes), past polls