Klem has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to read a file that I encrpted using the Crypt::Blowfish_PP module. My problem is that when I try to read the encrypted file using read() or sysread() they stop reading unexpectedly at 115 bytes even though -s tells me the file is 2458 bytes long. the code I am using to read is as follows
my $in; open IN, "out.txt" or die "can't open IN\n"; open OUT, ">unencrypted.txt" or die "can't open OUT\n"; my $len = (-s IN); print "len= $len\n"; #shows 2458 $len = sysread(IN, $in, (-s IN)); close IN; die "read= $len\n"; #shows 115 The file I am trying to read starts like this. 3߅m8&/3}{nş H1/߸R| 1/߸R| {n.3nRUW~H +I}n}֎`jCtZ^)X

Replies are listed 'Best First'.
Re: Preoblem Reading Encrypted file on a Win32 box
by tachyon (Chancellor) on Dec 20, 2001 at 23:33 UTC

    Try this:

    open IN, "out.txt" or die "Can't open out.txt for reading, Perl says $ +!"; print "Want ", (-s IN), "\n"; binmode IN; { local $/; # undef $/ so we read the whole file in $data = <IN>; } print "Got ", length $data;

    This will work fine. binmode() is required when dealing with binary files on Win32 to supress Perl's automagical line ending conversion. Typically you would use read() in a while() loop to grap a chunk of bytes to process. If you just want the whole file it is easier to just grab it as shown. Note that read() will not always reliably give you X bytes when you ask for X bytes...

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      binmode worked like a champ! Thanks for the help. KLEM
Re: Preoblem Reading Encrypted file on a Win32 box
by Aighearach (Initiate) on Dec 20, 2001 at 23:32 UTC
    You might need to
    binmode(IN); binmode(OUT);
    right after you open your files.
    --
    Snazzy tagline here