Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

To read Char-by-Char from a file

by Anonymous Monk
on May 12, 2005 at 10:59 UTC ( #456341=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks How do we read a file character by character without loading it into the memeory. Regards, Sid

Replies are listed 'Best First'.
Re: To read Char-by-Char from a file
by cog (Parson) on May 12, 2005 at 11:07 UTC
    As long as each character has one byte:

    $/ = \1;

    Assigning $/ to a reference to an integer makes it separate input on each $x bytes ($x here being the integer).

    Note: Bytes, not characters.

    Try it out:

    perl -pe 'BEGIN{$/=\1}s/.*/<$&>/' file

Re: To read Char-by-Char from a file
by gaal (Parson) on May 12, 2005 at 11:10 UTC
    my $read; while ($read = read FILE, $char, 1) { print "got: $char\n"; } die "read error: $!" if not defined $read;

    Note that this reads a character at a time, not a byte; that's not the same thing unless you're using a 7/8-bit encoding. See read for more details.

    Update: in terms of memory, this actually does read more than one byte at a time; but this is normally what you want. Perl performs buffering for you so you don't make many needless system calls. The buffer doesn't endanger your memory. See also sysread for the low-level call.

Re: To read Char-by-Char from a file
by rob_au (Abbot) on May 12, 2005 at 11:10 UTC
    If you have an open file handle, see perlfunc:getc, otherwise cog's suggestion is quite elegant (assuming your character-set is not "wide", requiring more than one byte per character).


    perl -le "print unpack'N', pack'B32', '00000000000000000000001000000000'"

Re: To read Char-by-Char from a file
by eXile (Priest) on May 12, 2005 at 13:41 UTC
      Absolutely. This has the added benefit to bypass Perl's buffered IO, which is what one really wants if (s)he needs to read a precise amount of data from a "source".

      In addition, if you want to read byte-by-byte instead of char-by-char (which is different due to Unicode support), it's safer to use binmode:

      binmode FILE; sysread(FILE,$buffer,1);

      Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

      Don't fool yourself.
Re: To read Char-by-Char from a file
by thcsoft (Monk) on May 12, 2005 at 12:23 UTC
    i wonder what you need that approach for? your question sounds like being a) a fake, or b) senseless.

    language is a virus from outer space.
      or, c) the pursuit of knowledge, which is never bad.
      who knows, there could easily be a good reason, too. maybe this is the only way to do something he's trying to do. or maybe it's not. if it's a good reason that could be done a different, better way, then maybe that's a mistake the op will have to make by doing it the wrong way first... also not always a bad thing.
      A fake question? Isn't that logically inconsistant? Something is either a question, or not, surely?

      my name's not Keith, and I'm not reasonable.
        Something is either a question, or not, surely?

        This is a fake answer.

        Perhaps what he meant by 'fake' was 'misrepresentative' as in the case of homework?

        Wait! This isn't a Parachute, this is a Backpack!
      Or c) correct, because (s)he really wants to read a file (descriptor) char-by-char or byte-by-byte.

      Think about a modem supervising application that waits for some defined input on the line and then hands over to another application chosen upon the seen input so far. Maybe if you read line-by-line you risk to eat too much input.

      Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

      Don't fool yourself.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://456341]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2018-01-17 19:07 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (203 votes). Check out past polls.