in reply to Need to read certain number of chars in a file

Dunno how you can fail to do this with a C prog -- this is one of the rare instances where it would probably be simpler.

Anyway, if you want to work with individual characters in perl here's some clues:

#!/usr/bin/perl -w use strict; open (IN, "<$0") || die; # $0 is this script my $data; read(IN,$data,30); # read in first 30 characters close(IN); # now split those into an array of individual values my @chars = unpack('c*',$data); print chr($_)."\n" foreach (@chars);

Replies are listed 'Best First'.
Re^2: Need to read certain number of chars in a file
by ranrodrig (Novice) on Oct 18, 2010 at 20:35 UTC

    Guys thanks a lot for your quick answer and you were rigth about the C program it was easier and simpler

    #include <stdio.h> main() { int c, i = 1; while (i <= 100){ c = getchar(); putchar(c); i++; } }

      The same in Perl:

      print getc() for 1..100;