Annotation of embedaddon/php/ext/standard/tests/file/fscanf_error.phpt, revision 1.1
1.1 ! misho 1: --TEST--
! 2: Test fscanf() function: error conditions
! 3:
! 4: --FILE--
! 5: <?php
! 6: /*
! 7: Prototype: mixed fscanf ( resource $handle, string $format [, mixed &$...] );
! 8: Description: Parses input from a file according to a format
! 9: */
! 10:
! 11: echo "*** Testing fscanf() for error conditions ***\n";
! 12: $file_path = dirname(__FILE__);
! 13:
! 14: $filename = "$file_path/fscanf_error.tmp";
! 15: $file_handle = fopen($filename, 'w');
! 16: if ($file_handle == false)
! 17: exit("Error:failed to open file $filename");
! 18: fwrite($file_handle, "hello world");
! 19: fclose($file_handle);
! 20:
! 21: // zero argument
! 22: var_dump( fscanf() );
! 23:
! 24: // single argument
! 25: $file_handle = fopen($filename, 'r');
! 26: if ($file_handle == false)
! 27: exit("Error:failed to open file $filename");
! 28: var_dump( fscanf($file_handle) );
! 29: fclose($file_handle);
! 30:
! 31: // invalid file handle
! 32: var_dump( fscanf($file_handle, "%s") );
! 33:
! 34: // number of formats in format strings not matching the no of variables
! 35: $file_handle = fopen($filename, 'r');
! 36: if ($file_handle == false)
! 37: exit("Error:failed to open file $filename");
! 38: var_dump( fscanf($file_handle, "%d%s%f", $int_var, $string_var) );
! 39: fclose($file_handle);
! 40:
! 41: // different invalid format strings
! 42: $invalid_formats = array( $undefined_var, undefined_constant,
! 43: "%", "%h", "%.", "%d%m"
! 44: );
! 45:
! 46:
! 47: // looping to use various invalid formats with fscanf()
! 48: foreach($invalid_formats as $format) {
! 49: $file_handle = fopen($filename, 'r');
! 50: if ($file_handle == false)
! 51: exit("Error:failed to open file $filename");
! 52: var_dump( fscanf($file_handle, $format) );
! 53: fclose($file_handle);
! 54: }
! 55:
! 56: echo "\n*** Done ***";
! 57: ?>
! 58: --CLEAN--
! 59: <?php
! 60: $file_path = dirname(__FILE__);
! 61: $filename = "$file_path/fscanf_error.tmp";
! 62: unlink($filename);
! 63: ?>
! 64: --EXPECTF--
! 65: *** Testing fscanf() for error conditions ***
! 66:
! 67: Warning: fscanf() expects at least 2 parameters, 0 given in %s on line %d
! 68: NULL
! 69:
! 70: Warning: fscanf() expects at least 2 parameters, 1 given in %s on line %d
! 71: NULL
! 72:
! 73: Warning: fscanf(): %d is not a valid File-Handle resource in %s on line %d
! 74: bool(false)
! 75:
! 76: Warning: fscanf(): Different numbers of variable names and field specifiers in %s on line %d
! 77: int(-1)
! 78:
! 79: Notice: Undefined variable: undefined_var in %s on line %d
! 80:
! 81: Notice: Use of undefined constant undefined_constant - assumed 'undefined_constant' in %s on line %d
! 82: array(0) {
! 83: }
! 84: array(0) {
! 85: }
! 86:
! 87: Warning: fscanf(): Bad scan conversion character " in %s on line %d
! 88: NULL
! 89:
! 90: Warning: fscanf(): Bad scan conversion character " in %s on line %d
! 91: NULL
! 92:
! 93: Warning: fscanf(): Bad scan conversion character "." in %s on line %d
! 94: NULL
! 95:
! 96: Warning: fscanf(): Bad scan conversion character "m" in %s on line %d
! 97: NULL
! 98:
! 99: *** Done ***
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>