Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > &read_file in File::Slurp terminates unexpectedly on file

Reply
Thread Tools

&read_file in File::Slurp terminates unexpectedly on file

 
 
Charles R. Thompson
Guest
Posts: n/a
 
      01-12-2004
I'm working through the conversion of some fixed-length record files with
extended ASCII data and a series of characters in the some of the files
appears to be causing read_file to assume it's at the end of the file. These
won't translate in the various readers so I'll notate. My hex editor says
the last characters where it terminates are:

00 C7 07 CA 1A 29 00

I see everything up to the 1A (Decimal 26), meaning I can see the CA as the
end. According to an ASCII chart I found online 1A is the 'substitute'
character.

Is there a method in Perl I can use to ensure an entire file is read so I
can read every character without incident?

Charles


 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      01-12-2004

"Charles R. Thompson" <(E-Mail Removed)> wrote:
> I'm working through the conversion of some fixed-length record files with
> extended ASCII data and a series of characters in the some of the files
> appears to be causing read_file to assume it's at the end of the file. These
> won't translate in the various readers so I'll notate. My hex editor says
> the last characters where it terminates are:
>
> 00 C7 07 CA 1A 29 00
>
> I see everything up to the 1A (Decimal 26), meaning I can see the CA as the
> end. According to an ASCII chart I found online 1A is the 'substitute'
> character.
>
> Is there a method in Perl I can use to ensure an entire file is read so I
> can read every character without incident?


Have you called binmode() on the filehandle concerned?

Ben

--
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
http://www.velocityreviews.com/forums/(E-Mail Removed) <=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=> (Kate Rusby)
 
Reply With Quote
 
 
 
 
Jay Tilton
Guest
Posts: n/a
 
      01-12-2004
"Charles R. Thompson" <(E-Mail Removed)> wrote:

: My hex editor says
: the last characters where it terminates are:
:
: 00 C7 07 CA 1A 29 00
:
: I see everything up to the 1A (Decimal 26), meaning I can see the CA as the
: end. According to an ASCII chart I found online 1A is the 'substitute'
: character.

On DOS-ish filesystems, character 0x1A marks the end-of-file when
reading the file as text.

: Is there a method in Perl I can use to ensure an entire file is read so I
: can read every character without incident?

binmode() the filehandle. This will screw up the normal CRLF
translation, but that's easily remedied.

 
Reply With Quote
 
Trent Curry
Guest
Posts: n/a
 
      01-13-2004
Jay Tilton wrote:
> "Charles R. Thompson" <(E-Mail Removed)> wrote:
>
>> My hex editor says
>> the last characters where it terminates are:
>>
>> 00 C7 07 CA 1A 29 00
>>
>> I see everything up to the 1A (Decimal 26), meaning I can see the CA
>> as the end. According to an ASCII chart I found online 1A is the
>> 'substitute' character.

>
> On DOS-ish filesystems, character 0x1A marks the end-of-file when
> reading the file as text.


Yes, on my WinXP Pro system if I insert 0x1A (Ctrl + Z) in the middle
file and read it without binmode() it gets cut off there.

Just FYI, the same is not true in a unix/linux based envirornment. 0x04
(Ctrl + D) and 0x03 (Ctrl + C) characters insert into the file does not
prevent reading to the end. It is my understnading that this is a Win32
quirk (at least NT based; I have no Win9x/ME systems to check with.)

>> Is there a method in Perl I can use to ensure an entire file is read
>> so I can read every character without incident?

>
> binmode() the filehandle. This will screw up the normal CRLF
> translation, but that's easily remedied.


It still read just fine, but if you want the end result to be just \n
(LF) instead of \r\n (CRLF) a simple

$line = s!\r\n!\n!g;

for each line oughtta do it.

Or one better:

$line = s!\r\n|\r!\n!g;

(Or if you don't find reading the whole file to memory

local $/ = undef;
(my $file = <SOMEFILE>) =~ s!\r\n|\r!\n!g;

Though if you know the file will large linebe line is best suited, and
usually the way to go in most cases.

--
Trent Curry

perl -e
'($s=qq/e29716770256864702379602c6275605/)=~s!([0-9a-f]{2})!pack("h2",$1
)!eg;print(reverse("$s")."\n");'


 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      01-13-2004
>>>>> "BM" == Ben Morrow <(E-Mail Removed)> writes:

BM> "Charles R. Thompson" <(E-Mail Removed)> wrote:

>> Is there a method in Perl I can use to ensure an entire file is read so I
>> can read every character without incident?


BM> Have you called binmode() on the filehandle concerned?

and you can enable binmode when using File::Slurp (a recent
version). the older module couldn't do binmode nor an already open
handle that had binmode called on it.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Charles R. Thompson
Guest
Posts: n/a
 
      01-13-2004
> >> Is there a method in Perl I can use to ensure an entire file is read
so I
> >> can read every character without incident?


> and you can enable binmode when using File::Slurp (a recent
> version). the older module couldn't do binmode nor an already open
> handle that had binmode called on it.


I am using an older version, you are correct. I found the binmode answer
earlier after searching more on "1A" and Perl. I have to say after that
searching I found an alarming number of posts with my same problem. I had
previously went to the FAQs first and tried all the examples under "How can
I read in an entire file all at once? " hoping one of them provided a clue,
no dice.

Even though this appears to be Windows specific, I think including a note on
binmode in that particular FAQs section would be very beneficial. Not a cop
out... I fully realize now searching a bit more with some specifics would
have gotten my answer, but I also wouldn't have spent my time and others
here if it were in the FAQs.

Just a thought.

Charles


 
Reply With Quote
 
Ben Liddicott
Guest
Posts: n/a
 
      01-13-2004
You need to use the binmode function, or the three argument open, with O_BINARY.

--
Cheers,
Ben Liddicott

"Charles R. Thompson" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...

> 00 C7 07 CA 1A 29 00
>
> I see everything up to the 1A (Decimal 26), meaning I can see the CA as the
> end. According to an ASCII chart I found online 1A is the 'substitute'
> character.
>
> Is there a method in Perl I can use to ensure an entire file is read so I
> can read every character without incident?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot read from Runtime.getRuntime().exec().getErrorStream because process terminates Zsolt Koppany Java 2 03-05-2011 05:51 PM
Debian MySQL Perl DBI - connection terminates unexpectedly after 100 secs. John Perl Misc 10 03-26-2009 03:42 PM
Cisco Router to VPN 3000 Tunnel Terminates Every 10 minutes or so. HELP! Rick B. Cisco 3 01-16-2004 04:53 PM
VPN terminates on pix 501...but i cannot access windows 2000 vpn server Warren Turner Cisco 0 01-09-2004 08:28 PM
Server terminates on opening mail Michael Hammett Computer Support 3 01-06-2004 07:37 PM



Advertisments