ファイルの COPY
1.1. ファイルの COPY
1行ずつ読んで、1行ずつ書く
lesson001.php
<?php
$fp = fopen("php://stdin", "r");while (!feof($fp))
{
print(fgets($fp));
}fclose($fp);
?>
実行結果
C:\>php "C:\study\SoftwareTools\1_1_ファイルのCOPY\lesson001.php" < "C:\study\So
ftwareTools\1_1_ファイルのCOPY\test.txt"
1. 手はじめに1.1. ファイルの COPY
1.2. 文字数を数える
1文字ずつ読んで、1文字ずつ書く
これでは、だめ
lesson002.php
<?php
$fp = fopen("php://stdin", "r");while (!feof($fp))
{
echo fgetc($fp), "\n";
}fclose($fp);
?>
実行結果
C:\>php "C:\study\SoftwareTools\1_1_ファイルのCOPY\lesson002.php" < "C:\study\So
ftwareTools\1_1_ファイルのCOPY\test.txt"1
.
ヘ
カ
゚
ノ
1
.
1
.
t
@
C
フ
C
O
P
Y
1
.
2
.
カ
ヲ
1文字ずつ読んで、1文字ずつ書く
lesson003.php
<?php
$current_line = "";
$pos = -1;
$c = "";$fp = fopen("php://stdin", "r");
while (!feof($fp))
{
# 1行ずつ読む
$current_line = fgets($fp);# 改行コードを取り除く
$current_line = rtrim($current_line, "\n\r");$pos = 0;
while ($pos < strlen($current_line))
{
# 半角 / 全角
if (mb_ereg('^[\x80-\xff]', substr($current_line, $pos, 1)))
{
# 2バイト取得
$c = substr($current_line, $pos, 2);
$pos += 2;
}
else
{
# 1バイト取得
$c = substr($current_line, $pos, 1);
$pos++;
}# 1文字表示
echo $c, "\n";
}
}fclose($fp);
?>
実行結果
C:\>php "C:\study\SoftwareTools\1_1_ファイルのCOPY\lesson003.php" < "C:\study\So
ftwareTools\1_1_ファイルのCOPY\test.txt"1
.手
は
じ
め
に
1
.
1
.フ
ァ
イ
ル
のC
O
P
Y
1
.
2
.文
字
数
を
数
え
る
1文字ずつ読んで、1文字ずつ書く
lesson004.php
<?php
#******************************************************************************
# 1文字ずつ読んで、1文字ずつ書く
#******************************************************************************
$c = "";
$EOF = "\0";
$current_line = "";
$pos = -1;
$NEWLINE = "\n";
$buffer = "";while (($c = get_char()) != $EOF)
{
put_char($c);
}
#==============================================================================
# 1文字 取得
#==============================================================================
function get_char()
{
global $fp;
global $EOF;global $current_line;
global $pos;
global $NEWLINE;$char = "";
# まだ読んでなかったら
if ($current_line == "")
{
# まだ OPEN してなかったら
if ($pos == -1)
{
$fp = fopen("php://stdin", "r");
}# 1行読む
$current_line = fgets($fp);# ファイルの終わりなら 終了
if (feof($fp))
{
fclose($fp);
return $EOF;
}# 改行コードを取り除く
$current_line = rtrim($current_line, "\n\r");# 現在位置 クリア
$pos = 0;
}# 行の終わりに達したら
if ($pos >= strlen($current_line))
{
# 現在行 クリア
$current_line = "";# 行の終わりを 知らせる
return $NEWLINE;
}# 半角 / 全角
if (mb_ereg('^[\x80-\xff]', substr($current_line, $pos, 1)))
{
# 2バイト取得
$char = substr($current_line, $pos, 2);
$pos += 2;
}
else
{
# 1バイト取得
$char = substr($current_line, $pos, 1);
$pos++;
}return $char;
}#==============================================================================
# 1文字 出力
#==============================================================================
function put_char($char)
{
global $NEWLINE;
global $buffer;if ($char == $NEWLINE)
{
# 行の終わりなら 出力
echo $buffer, "\n";
$buffer = "";
}
else
{
# 行の終わりでなければ、バッファにためる
$buffer .= $char;
}
}
?>
実行結果
C:\>php "C:\study\SoftwareTools\1_1_ファイルのCOPY\lesson004.php" < "C:\study\So
ftwareTools\1_1_ファイルのCOPY\test.txt"
1. 手はじめに1.1. ファイルの COPY
1.2. 文字数を数える