For C/C++ programmers.
fscanf() does not work like C/C++, because PHP's fscanf() move file pointer the next line implicitly.
(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)
fscanf — フォーマットに基づきファイルからの入力を処理する
関数fscanf() は sscanf()
に似ていますが、stream
が指すファイルから入力を取得し、
指定したフォーマット format
に基づき解釈を行います。
フォーマットについては sprintf() に解説されています。
フォーマット文字列におけるあらゆる空白は
入力ストリームのあらゆる空白にマッチします。
これはつまりフォーマット文字列の\t
(タブ)すらも
入力ストリームの空白1個にマッチしてしまうことを意味します。
fscanf() をコールするたびに、ファイルから 1 行ずつ読み込みます。
stream
fopen() を使用して作成したファイルシステムポインタリソース。
format
0個以上のディレクティブで構成されるフォーマット文字列:
変換結果に直接コピーされる通常文字列 (%
は除きます)
と、変換仕様。
これらのいずれも、自分が持つパラメータを取得します。
変換の仕様は、以下のプロトタイプに従います:
%[argnum$][flags][width][.precision]specifier
.
何番目の引数を変換の対象にするかを指定するために、
数値の後にドル記号 $
を続けます。
フラグ | 説明 |
---|---|
- |
与えられたフィールドの幅を左寄せにします。 右寄せがデフォルトです。 |
+ |
正の数値の前に付ける + 符号です;
デフォルトは、負の数にだけマイナスの符号が数値の前に付きます。
|
(space) |
スペースに変換される詰め物です。 これがデフォルトです。 |
0 |
数値の左側を0埋めします。
s 指定子を使うと、
右側にも0埋めできます。
|
' (char) |
指定された (char) で埋めます。 |
(最小で)何文字がこの変換結果に含まれるかを数値で指定します。
ピリオド .
の後に数値を続けますが、
その意味は指定子に依存します:
e
, E
,
f
と F
指定子の場合:
小数点の後に表示する桁数 (デフォルトでは、この値は6です)
g
, G
,
h
, H
指定子の場合:
表示する最大の有効桁数
s
指定子の場合:
文字列を切り捨てる時点、つまり、文字列の最大の長さを設定します。
注意: 明示的に精度を指定せず、ピリオドを指定した場合、精度は0として扱われます。
注意: 位置を示す指定子に、
PHP_INT_MAX
を超える値を指定すると、警告が発生します。
指定子 | 説明 |
---|---|
% |
文字通り、パーセント文字です。 引数は不要です。 |
b |
引数は数値として扱われ、バイナリ値として表現されます。 |
c |
引数は数値として扱われ、ASCII文字として表現されます。 |
d |
引数は数値として扱われ、(符号付き)小数値として表現されます。 |
e |
引数は科学的記法で表現された値(e.g. 1.2e+2)として扱われます。 |
E |
e 指定子に似ていますが、
大文字を使います(e.g. 1.2E+2)
|
f |
引数は小数として扱われ、浮動小数値として表現されます(ロケールを考慮します)。 |
F |
引数は小数として扱われ、浮動小数値として表現されます(ロケールを考慮しません)。 |
g |
汎用フォーマット
P を精度を表す、ゼロでない値とします。
精度が省略された場合、Pの値は6です。
精度に0を指定した場合、Pの値は1になります。
この場合、
P > X ≥ −4 の場合、 |
G |
g 指定子に似ていますが、
E と f を使います。
|
h |
g 指定子に似ていますが、
F を使います。
PHP 8.0.0 以降で利用可能です。
|
H |
g 指定子に似ていますが、
E と F を使います。
PHP 8.0.0 以降で利用可能です。
|
o |
引数は数値として扱われ、8進数値として表現されます。 |
s |
引数は文字列として扱われ、文字列として表現されます。 |
u |
引数は数値として扱われ、符号なし小数値として表現されます。 |
x |
引数は数値として扱われ、16進数値(小文字)として表現されます。 |
X |
引数は数値として扱われ、16進数値(大文字)として表現されます。 |
c
指定子はパディングと幅を無視します
文字列と width 指定子を、1文字の表現に1バイト以上必要な文字セットと一緒に使おうとすると、 期待しない結果になるかもしれません。
値は、指定子の型に合うように強制されます:
型 | 指定子 |
---|---|
string | s |
int |
d ,
u ,
c ,
o ,
x ,
X ,
b
|
float |
e ,
E ,
f ,
F ,
g ,
G ,
h ,
H
|
vars
オプションで代入する値。
この関数のパラメータが二つだけの場合、処理された値は配列として返されます。 他方、オプションのパラメータが指定された場合、 この関数は、代入された値の数を返します。 オプション引数は参照渡しとする必要があります。
string
で利用可能な部分文字列よりも、
format
で期待された部分文字列の数が多い場合は、null
が返されます。
その他のエラーが発生した場合は、false
が返されます。
例1 fscanf() の例
<?php
$handle = fopen("users.txt", "r");
while ($userinfo = fscanf($handle, "%s\t%s\t%s\n")) {
list ($name, $profession, $countrycode) = $userinfo;
//... これらの値を使用して何か行う
}
fclose($handle);
?>
例2 users.txt の内容
javier argonaut pe hiroshi sculptor jp robert slacker us luigi florist it
For C/C++ programmers.
fscanf() does not work like C/C++, because PHP's fscanf() move file pointer the next line implicitly.
It would be great to precise in the fscanf documentation
that one call to the function, reads a complete line.
and not just the number of values defined in the format.
If a text file contains 2 lines each containing 4 integer values,
reading the file with 8 fscanf($fd,"%d",$v) doesnt run !
You have to make 2
fscanf($fd,"%d %d %d %d",$v1,$v2,$v3,$v4);
Then 1 fscanf per line.
If you want to parse a cron file, you may use this pattern:
<?php
while ($cron = fscanf($fp, "%s %s %s %s %s %[^\n]s"))
{
}
?>
Yet another function to read a file and return a record/string by a delimiter. It is very much like fgets() with the delimiter being an additional parameter. Works great across multiple lines.
function fgetd(&$rFile, $sDelim, $iBuffer=1024) {
$sRecord = '';
while(!feof($rFile)) {
$iPos = strpos($sRecord, $sDelim);
if ($iPos === false) {
$sRecord .= fread($rFile, $iBuffer);
} else {
fseek($rFile, 0-strlen($sRecord)+$iPos+strlen($sDelim), SEEK_CUR);
return substr($sRecord, 0, $iPos);
}
}
return false;
}
If you want to read text files in csv format or the like(no matter what character the fields are separated with), you should use fgetcsv() instead. When a text for a field is blank, fscanf() may skip it and fill it with the next text, whereas fgetcsv() correctly regards it as a blank field.
to include all type of visible chars you should try:
<?php fscanf($file_handler,"%[ -~]"); ?>
actually, instead of trying to think of every character that might be in your file, excluding the delimiter would be much easier.
for example, if your delimiter was a comma use:
%[^,]
instead of:
%[a-zA-Z0-9.| ... ]
Just make sure to use %[^,\n] on your last entry so you don't include the newline.
If you want fscanf()to scan one variable in a large number of lines, e.g an Ipadress in a line with more variables, then use fscanf with explode()
<?
$filename = "somefile.txt";
$fp = fopen($filename, "r") or die ("Error opening file! \n");
$u = explode(" ",$line); // $u is the variable eg. an IPadress
while ($line = fscanf($fp,"%s",$u)) {
if(preg_match("/^$u/",$_SERVER['REMOTE_ADDR'])) {$badipadresss++;} // do something and continue scan
}
?>
Besides, fscanf()is much faster than fgets()
fscanf works a little retardedly I've found. Instead of using just a plain %s you probably will need to use sets instead. Because it works so screwy compared to C/C++, fscanf does not have the ability to scan ahead in a string and pattern match correctly, so a seemingly perfect function call like:
fscanf($fh, "%s::%s");
With a file like:
user::password
Will not work. When fscanf looks for a string, it will look and stop at nothing except for a whitespace so :: and everything except whitespace is considered part of that string, however you can make it a little smarter by:
fscanf($fh, "%[a-zA-Z0-9,. ]::%[a-zA-Z0-9,. ]" $var1, $var2);
Which tells it that it can only accept a through z A through Z 0 through 9 a comma a period and a whitespace as input to the string, everything else cause it to stop taking in as input and continue parsing the line. This is very useful if you want to get a sentence into the string and you're not sure of exactly how many words to add, etc.
The use of PHP code in the ACM submission
Here is a sample solution for problem 1001 using PHP:
<?php
while (fscanf(STDIN, "%d%d", $a, $b) == 2) {
print ($a + $b) . "\n";
}