CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Woes with binmode


New User

Mar 19, 2008, 6:52 PM

Post #1 of 2 (794 views)
Woes with binmode Can't Post

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.



#!/usr/bin/perl -w 
use strict;
use vars qw/ $i $filename @line @splitdata @data $out /;
#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>);
#Read specific lines in while loop, split each part of each line, remove 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;

"To be conscious that you are ignorant is a great step towards knowledge" -- B. Disraeli


Mar 19, 2008, 7:39 PM

Post #2 of 2 (793 views)
Re: [sfashley] Woes with binmode [In reply to] Can't Post

I don't know the answer, I recommed you ask on


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives