Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
What's Wrong with "while ($c=fgetc($f)) {}"
What's Wrong with "while ($c=fgetc($f)) {}"? - PHP Script Tips - Reading and Writing Files
✍: FYIcenter.com
If you are using "while ($c=fgetc($f)) {}" to loop through each character in a file, the loop may end in the middle of the file when there is a "0" character, because PHP treats "0" as Boolean false. To properly loop to the end of the file, you should use "while ( ($c=fgetc($f)) !== false ) {}". Here is a PHP script example on incorrect testing of fgetc():
<?php $file = fopen("/temp/cgi.log", "w"); fwrite($file,"Remote host: 64.233.179.104.\r\n"); fwrite($file,"Query string: cate=102&order=down&lang=en.\r\n"); fclose($file); $file = fopen("/temp/cgi.log", "r"); while ( ($char=fgetc($file)) ) { print($char); } fclose($file); ?>
This script will print:
Remote host: 64.233.179.1
As you can see the loop indeed stopped at character "0".
2007-04-22, 4782👍, 0💬
Popular Posts:
What is difference between custom JSP tags and JavaBeans? Custom JSP tag is a tag you defined. You d...
How To Enter Microseconds in SQL Statements? - MySQL FAQs - Introduction to SQL Date and Time Handli...
Give me the example of SRS and FRS SRS :- Software Requirement Specification BRS :- Basic Requiremen...
How can we connect to Microsoft Access , Foxpro , Oracle etc ? Microsoft provides System.Data.OleDb ...
What are shared (VB.NET)/Static(C#) variables? Static/Shared classes are used when a class provides ...