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

Reading from the Perl Blackbook it says you can read a certain amount of data from a file. For some reason, it reads and prints the entire thing even though I only tell it to print 1 byte! The test file is about 2 pages long, definitely larger than a byte but everything is printing.
open(TEST, "test.txt") or die "oops: $!"; my $text = ""; while(read (TEST, $stuff, 1)) { $text .= $stuff; } print $text;
Then, I tried adding an offset of 5 thinking it will skip the first portion of it, right? Instead of printing the text, it jumped it all up
H T M L f o r m + a t ( . h t m o r . h + t m l ) t e x t / h t m + l A d o b e P o + r t a b l e D o c u m e n + t ( p d f ) a p p l i c a t i + o n / p d f M i c r o s o f + t W o r d ( . d o c ) a p p l i c a t i + o n / m s w o r d M i c r o s o f + t E x c e l ( . x l s ) a p p l i c a t i + o n / m s e x c e l M i c r o s o f + t P o w e r p o i n t ( + . p p t ) a p p l i c a t i + o n / m s - p o w e r p o i + n t R e a l a u d i + o ( . r m , . r a m ) a u d i o / x - + p n - r e a l a u d i o T e x t f o r + m a t ( . t x t ) t e x t / t x t Z i p p e d f i l e + s ( . z i p ) a p p l i c a t i o + n / z i p . a v i v i d + e o / a v i . b m p i m a + g e / b m p . g i f i m a + g e / g i f . g z i p a p p l i + c a t i o n / x - g z i p . j p g i m + a g e / j p e g . j s a p p + l i c a t i o n / x - j a v + a s c r i p t . m i d i a + p p l i c a t i o n / x - m + i d i . m p 3 a u + d i o / m p e g 3 . m p e g v i + d e o / m p e g . p n g i m a + g e / p n g . p p t a p p + l i c a t i o n / m s p o w + e r p o i n t . p s d a p p + l i c a t i o n / o c t e t + - s t r e a m . z i p a p p l i c + a t i o n / z i p . d o c a p p + l i c a t i o n / m s w o r + d = = = = = = = = = = = + = = = = = = = = = = = = = = + = = = = = = = = = = = = = + = = = = A D D L e a f 4 2 : b + i n L e a f 4 2 : c + u e L e a f 4 2 : r + a r L e a f 4 2 : w + m v L e a f 4 2 : m + d b
What in the world am I doing wrong?

Replies are listed 'Best First'.
Re: Reading input byte by byte
by leira (Monk) on Apr 17, 2004 at 02:50 UTC
    For some reason, it reads and prints the entire thing even though I only tell it to print 1 byte!

    Actually, you're telling it to read one byte at at time, until there's nothing left (read returns the number of bytes it succeeded in reading -- when there are none left, it will return 0, and your while loop will stop):

    while(read (TEST, $stuff, 1)) {

    ...and then you're appending what you read during this pass onto what you've already read, until $text contains everything in the file:

    $text .= $stuff;

    ....and then you're printing out the whole thing:

    print $text;

    If you only want to read 1 byte and then stop, you probably don't want to put it in a while loop.

    Linda

Re: Reading input byte by byte
by graff (Chancellor) on Apr 17, 2004 at 05:48 UTC
    You didn't actually show the version of the code that produced the "spaced-out" listing of the data, and it's not really clear what you meant, exactly, when you said:
    I tried adding an offset of 5 thinking it will skip the first portion of it...
    In what sense (using what changes to the original code) did you "add an offset of 5"? You seem to be getting 5 spaces between each character, or perhaps it's 5 null bytes between each character. (I think the "Command-prompt" windows on MS systems tend to show a space whenever the "text" being displayed has a null byte.)

    Anyway, as inidicated in the first reply, if you only want to see one byte of output, don't use "while (...)" to go through the whole file. On the other hand, if you want to go through the whole file, but you want to do something distinctly with each byte, do that thing inside the while loop, and don't bother concatenating all the bytes back together into a single scalar string. For example, if you just wanted to print one byte per line:

    while ( read( TEST, $stuff, 1 )) { print $stuff,"\n"; }