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:
How To Read One Line of Text from a File
How To Read One Line of Text from a File? - PHP Script Tips - Reading and Writing Files
✍: FYIcenter.com
If you have a text file with multiple lines, and you want to read those lines one line at a time, you can use the fgets() function. It reads the current line up to the "\n" character, moves the file pointer to the next line, and returns the text line as a string. The returning string includes the "\n" at the end. Here is a PHP script example on how to use fgets():
<?php $file = fopen("/windows/system32/drivers/etc/services", "r"); while ( ($line=fgets($file)) !== false ) { $line = rtrim($line); print("$line\n"); # more statements... } fclose($file); ?>
This script will print:
# This file contains port numbers for well-known services echo 7/tcp ftp 21/tcp telnet 23/tcp smtp 25/tcp ...
Note that rtrim() is used to remove "\n" from the returning string of fgets().
2007-04-22, 4988👍, 0💬
Popular Posts:
Why does malloc(0) return valid memory address? What's the use? malloc(0) does not return a non-NULL...
How To Truncate an Array? - PHP Script Tips - PHP Built-in Functions for Arrays If you want to remov...
Can you explain project life cycle ? Figure :- 12.2 Life cycle of a project There are five stages of...
Describe in detail Basic of SAO architecture of Remoting? For these types of questions interviewer e...
How do you locate the first X in a string txt? A) txt.find('X'); B) txt.locate('X'); C) txt.indexOf(...