getc関数 - ファイルから一文字ずつ読み込む



  1. Perl




  2. 関数



  3. here

 getc関数を使って1文字づつ読み込むこともできます。

my $c = getc $fh;

サンプル

 以下は、1文字読み込みのサンプルです。

use strict;
use warnings;

# 1文字読み込み
# 読み込みたいファイル名
my $file = shift;

print "1: 1文字ずつ読み込む。\n";
open(my $fh, "<", $file)
  or die "Cannot open $file: $!";

# 1文字ずつ読み込むにはgetc関数を使います。
while (defined (my $c = getc $fh)) {
  print $c;
}

close $fh;



  1. Perl




  2. 関数



  3. here