Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP
i want to check if the end of line is reached but when i run
 
the code it doesn't show anything although the file has contents
my code is as following
 

 <?php
    
    /**
     * @author gencyolcu
     * @copyright 2013
     */
    
    $file=fopen("m.txt","r+") or exit("couldn't open file");
    
    while(!PHP_EOL)
    {
        echo fgets($file). "<br />";
    }
    
    
    fclose($file);
    
    ?>
Posted 28-Apr-13 17:44pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
 
You can feof function to check the end of file. Please see the modified code below.
$file = fopen("m.txt","r+") or exit("couldn't open file");
while(!feof($file) {
    echo fgets($file) . "<br />";
}
fclose($file);
For fgets function if you do not specify the length then it will keep reading from the stream until it reaches the end of the line.
 
To read a file line by line see code below.
You could try the following way
<?php
$file1 = "G:\\Events.txt";
$lines = file($file1);
foreach($lines as $line_num =?>
OR
<?php $strFile = "G:\\Events.txt";
$hFile = fopen($strFile, "r");
while (($line = fgets($hFile)) !== false) {
    echo $line;
}
?>
Regards,
 
Regards,
  Permalink  
v2
Comments
TheSniper105 at 29-Apr-13 0:49am
   
is there any way to check end of the line not file??
TheSniper105 at 29-Apr-13 1:50am
   
could u please format ur code can't read it
Prasad Khandekar at 29-Apr-13 1:44am
   
I have updated the solution, it now includes the code for reading line by line as well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Marcin Kozub 330
1 OriginalGriff 256
2 Sergey Alexandrovich Kryukov 215
3 Praneet Nadkar 197
4 Richard MacCutchan 182
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100