xref: /PHP-8.0/ext/standard/tests/file/fscanf.phpt (revision 5dafd7b4)
1--TEST--
2fscanf() tests
3--FILE--
4<?php
5
6$filename = __DIR__."/fscanf.dat";
7
8file_put_contents($filename, "data");
9
10$fp = fopen($filename, "rt");
11var_dump(fscanf($fp, "%d", $v));
12var_dump($v);
13fclose($fp);
14
15$fp = fopen($filename, "rt");
16var_dump(fscanf($fp, "%s", $v));
17var_dump($v);
18fclose($fp);
19
20$fp = fopen($filename, "rt");
21try {
22    fscanf($fp, "%s", $v, $v1);
23} catch (ValueError $exception) {
24    echo $exception->getMessage() . "\n";
25}
26var_dump($v);
27var_dump($v1);
28fclose($fp);
29
30$v = array();
31$v1 = array();
32$fp = fopen($filename, "rt");
33try {
34    fscanf($fp, "", $v, $v1);
35} catch (ValueError $exception) {
36    echo $exception->getMessage() . "\n";
37}
38var_dump($v);
39var_dump($v1);
40fclose($fp);
41
42$v = array();
43$v1 = array();
44$fp = fopen($filename, "rt");
45try {
46    fscanf($fp, "%.a", $v, $v1);
47} catch (ValueError $exception) {
48    echo $exception->getMessage() . "\n";
49}
50var_dump($v);
51var_dump($v1);
52fclose($fp);
53
54@unlink($filename);
55touch($filename);
56
57$fp = fopen($filename, "rt");
58var_dump(fscanf($fp, "%s", $v));
59var_dump($v);
60fclose($fp);
61
62file_put_contents($filename, "data");
63
64$fp = fopen($filename, "rt");
65try {
66    var_dump(fscanf($fp, "%s%d", $v));
67} catch (ValueError $exception) {
68    echo $exception->getMessage() . "\n";
69}
70
71echo "Done\n";
72?>
73--CLEAN--
74<?php
75$filename = __DIR__."/fscanf.dat";
76unlink($filename);
77?>
78--EXPECT--
79int(0)
80NULL
81int(1)
82string(4) "data"
83Variable is not assigned by any conversion specifiers
84string(4) "data"
85NULL
86Variable is not assigned by any conversion specifiers
87array(0) {
88}
89array(0) {
90}
91Bad scan conversion character "."
92array(0) {
93}
94array(0) {
95}
96bool(false)
97array(0) {
98}
99Different numbers of variable names and field specifiers
100Done
101