note 14426 modified in function.fopen by sander

From: Date: Sun, 07 Apr 2002 11:14:23 +0000
Subject: note 14426 modified in function.fopen by sander
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-28830@lists.php.net to get a copy of this message
I was working on a consol script for win32 and noticed a few things about it. On win32 it appears that you can't re-open the input stream for reading, but rather you have to open it once, and read from there on. Also, i don't know if this is a bug or what but it appears that fgets() reads until the new line anyway. The number of characters returned is ok, but it will not halt reading and return to the script. I don't know of a work around for this right now, but i'll keep working on it. This is some code to work around the close and re-open of stdin. <?php function read($length='255'){ if (!isset($GLOBALS['StdinPointer'])){ $GLOBALS['StdinPointer']=fopen("php://stdin","r"); } $line=fgets($GLOBALS['StdinPointer'],$length); return trim($line); } echo "Enter your name: "; $name=read(); echo "Enter your age: "; $age=read(); echo "Hi $name, Isn't it Great to be $age years old?"; @fclose($StdinPointer); ?> --was-- <p>I was working on a consol script for win32 and noticed a few things about it. On win32 it appears that you can't re-open the input stream for reading, but rather you have to open it once, and read from there on. Also, i don't know if this is a bug or what but it appears that fgets() reads until the new line anyway. The number of characters returned is ok, but it will not halt reading and return to the script. I don't know of a work around for this right now, but i'll keep working on it.</p> This is some code to work around the close and re-open of stdin. <?php function read($length='255'){ if (!isset($GLOBALS['StdinPointer'])){ $GLOBALS['StdinPointer']=fopen("php://stdin","r"); } $line=fgets($GLOBALS['StdinPointer'],$length); return trim($line); } echo "Enter your name: "; $name=read(); echo "Enter your age: "; $age=read(); echo "Hi $name, Isn't it Great to be $age years old?"; @fclose($StdinPointer); ?> http://www.php.net/manual/en/function.fopen.php

« previous php.notes (#28830) next »