15c02-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 47 行

PHP
47
字号
<?php// We are going to read/write from a file that has the following format:// line 1 will be a 7 digit number (zero padded) followed immediately by//    a string of any length.// line 2 will be a 3 character code, followed by *, then a left-justified//    string no longer than 40 characters, a space, and then a float value// Define the data that we will be writing:$num = 27;$str = 'An entry into this data file';$code = 'AxH';$left = 'Data Validated';$val = 5.6789;// Now open a data file to write these out to:$dat = fopen('test.dat', 'w');// And put this data into it in the custom format described:// First line, a zero padded 7 digit number followed immediately by a stringfprintf($dat, "%07d%s\n", $num, $str);// Second line, 3 character code, *, 40 char left-just string, space, floatfprintf($dat, "%3s*%-40s %f\n", $code, $left, $val);// Now close the file.fclose($dat);// Now let's dump the file to the screen to see what it looks like:echo "<pre>File Contents:\n";readfile('test.dat');// Now let's open it back up to read from it:$readdat = fopen('test.dat', 'r');echo "\n\nFile Data:\n";// Read back in the first line using a similar format string.fscanf($readdat, "%07d%[^\n]", $n1, $s2);echo "Number = '{$n1}'\nString = '{$s2}'\n";// And now the second line:fscanf($readdat, "%3s*%40[^\n] %f\n", $c3, $l4, $v5);echo "Code = '{$c3}'\nLeft-String = '{$l4}'\nFloat = '{$v5}'";echo "</pre>";// Close the file:fclose($readdat);?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?