Categories:
.NET (961)
C (387)
C++ (185)
CSS (84)
DBA (8)
General (31)
HTML (48)
Java (641)
JavaScript (220)
JSP (109)
JUnit (31)
MySQL (297)
Networking (10)
Oracle (562)
Perl (48)
Perl (9)
PHP (259)
PL/SQL (140)
RSS (51)
Software QA (28)
SQL Server (5)
Struts (20)
Unix (2)
Windows (3)
XHTML (199)
XML (59)
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, 4911👍, 0💬
Popular Posts:
How To Set session.gc_maxlifetime Properly? - PHP Script Tips - Understanding and Using Sessions As ...
What will be printed as the result of the operation below: main() { int x=5; printf("%d,%d,%d\n",x,x. ..
How To Define a Data Source Name (DSN) in ODBC Manager? - Oracle DBA FAQ - ODBC Drivers, DSN Configu...
What is shadowing ? When two elements in a program have same name, one of them can hide and shadow t...
Which bit wise operator is suitable for checking whether a particular bit is on or off? The bitwise ...