Dear Monks,

I have been re-directed from http://perlguru.com with regards to the following question. I have been attempting to write a code that reads a simple ASCII file and outputs it in a binary format. The problem comes that the outputted file is not in binary format (it is still in ASCII format, and this is backed up doing od -c filename).

I have tried using binmode <filehandle>, ":raw"; and also open (<fh>, ">:raw", $filename); but to no avail. Any suggestions as to what I am doing wrong? I enclose the rather primitive code at the bottom of this message.

Help regarding this matter would be very much appreciated. I have tried it on two separate versions of Linux, one based at home which is FC7 and the other is a version of CentOS. My hone version does have PerlIO.pm installed which makes it all the more perplexing. If you would like a copy of the input file, please let me know and I'll make it available.

Regards

Steve

#!/usr/bin/perl -w use strict; use vars qw/ $i $filename @line @splitdata @data $out /; $i=1649; #Obtain name of file printf ("Please enter the name of file which you would like converted\ +n"); chomp ($filename=<STDIN>); printf "Filename inputted was %s\n", $filename; #Open data file, rid the new lines and store it to the line array open ( DATAFILE, "<", "$filename"); chomp (@line = <DATAFILE>); close DATAFILE; #Read specific lines in while loop, split each part of each line, remo +ve whitespace via pop at the start and the end of the line and store +it to a new array called data while (defined($line[$i]) && $i < 3288){ @splitdata = split (/\s+/,$line[$i]); pop @splitdata; @splitdata =reverse(@splitdata); pop @splitdata; @splitdata =reverse(@splitdata); push @data, @splitdata; $i = $i + 1; } #Open an outfile and write the contents of data, in hexadecimal, into +a binary format open (OUTFILE, ">","newfile"); binmode OUTFILE, ":raw"; foreach $out (@data){ printf OUTFILE ("%08x\n", $out); } close OUTFILE;

In reply to Woes with binmode by sf_ashley

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.