2 different outputs of read, simple code but strange output.

Discussion in 'Perl Misc' started by newsgroupie2003@yahoo.dk, Nov 26, 2005.

  1. Guest

    Hi,
    I had trouble with a Perl parsing program not working on two different
    machines. I have now boiled it down to the minimal example.
    Basically i run the code below on 1) fedora core 3, Perl v5.8.5 and
    on 2) Linux ret hat Enterp. Perl v5.8.0.
    The code reads 4 bytes from the same file and the outputs are:
    1)
    length= 156
    0
    0
    9
    0

    2)
    length= 156
    0
    0
    9
    114



    As bytes are read i can't see how it can be related to endianess.
    Does anyone have an idea what could be wrong?

    Any help greatly appreciated as i am a bit out of ideas of how to
    triangulate the error further.

    Cheers
    Soren




    CODE:

    if (!open(GEF, "<".$filename)) {
    warn "Can't open file $filename: $!\n";
    return 1;
    }

    print "length= ".read(GEF, $pixel_hdr, 156). "\n";

    my $q;
    for ($q=0; $q < 4; $q++)
    {
    my $tempstring = substr($pixel_hdr, 124+$q, 1);
    print unpack("c", $tempstring);
    print "\n";
    }
     
    , Nov 26, 2005
    #1
    1. Advertising

  2. robic0 Guest

    On 26 Nov 2005 01:16:37 -0800, wrote:

    Well your opening a file, reading a block into a buffer
    then printing a few characters. I know this answer in
    ansi C but I'm not sure here. Did you change the read
    mode to binary after you opened it?


    >Hi,
    > I had trouble with a Perl parsing program not working on two different
    >machines. I have now boiled it down to the minimal example.
    >Basically i run the code below on 1) fedora core 3, Perl v5.8.5 and
    >on 2) Linux ret hat Enterp. Perl v5.8.0.
    >The code reads 4 bytes from the same file and the outputs are:
    >1)
    > length= 156
    >0
    >0
    >9
    >0
    >
    >2)
    >length= 156
    >0
    >0
    >9
    >114
    >
    >
    >
    >As bytes are read i can't see how it can be related to endianess.
    >Does anyone have an idea what could be wrong?
    >
    >Any help greatly appreciated as i am a bit out of ideas of how to
    >triangulate the error further.
    >
    >Cheers
    >Soren
    >
    >
    >
    >
    >CODE:
    >
    > if (!open(GEF, "<".$filename)) {
    > warn "Can't open file $filename: $!\n";
    > return 1;
    > }
    >
    > print "length= ".read(GEF, $pixel_hdr, 156). "\n";
    >
    > my $q;
    > for ($q=0; $q < 4; $q++)
    > {
    > my $tempstring = substr($pixel_hdr, 124+$q, 1);
    > print unpack("c", $tempstring);
    > print "\n";
    > }
     
    robic0, Nov 26, 2005
    #2
    1. Advertising

  3. <> wrote:

    > I had trouble with a Perl parsing program not working on two different
    > machines. I have now boiled it down to the minimal example.



    > print "length= ".read(GEF, $pixel_hdr, 156). "\n";



    Are you using utf8 on one machine but not on the other?

    See the last paragraph of:

    perldoc -f read


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 26, 2005
    #3
  4. Guest

    Thanks!
    The RH9 has UTF set under locale. I used binmode(FH) as is also
    recommended elsewhere in this group and it works without problems.

    Cheers
    Soren
     
    , Nov 27, 2005
    #4
  5. <> wrote:

    > Thanks!



    Thanks for what?

    Please quote some context in followups like everybody else does.


    (You're welcome, BTW)


    > The RH9 has UTF set under locale.



    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 27, 2005
    #5
    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. Raja Kiran Kumar Reddy Sandireddy

    Different outputs in different environments.

    Raja Kiran Kumar Reddy Sandireddy, Sep 1, 2004, in forum: C Programming
    Replies:
    13
    Views:
    616
    Keith Thompson
    Sep 9, 2004
  2. Casanova

    different outputs in gcc and tc

    Casanova, Mar 19, 2006, in forum: C Programming
    Replies:
    9
    Views:
    319
    John Bode
    Mar 19, 2006
  3. pradeep
    Replies:
    3
    Views:
    836
    cwdjrxyz
    Jun 7, 2007
  4. ram
    Replies:
    4
    Views:
    288
  5. pradeep
    Replies:
    3
    Views:
    145
    Dr J R Stockton
    Jun 7, 2007
Loading...

Share This Page