Reply to Re: Read first 10 lines from a file

Your name:

Reply:


Posted by Stefan Rybacki on 10/10/53 11:24

Tyrone Slothrop wrote:
>...
>
> Use the file function instead:
> $lines = file('file.txt');
> for ($i=0; $i<10; $i++) {
> $field = split("\t", $lines);
> echo $f[0]]." - ".$f[1] ;
> }
>
>

Robertico keep track of the file size, when using this because the file is read at once.
Your approach is better for big files, just add a counting variable and make your if a
loop then break after 10 steps.

$fd = fopen($fname, "r");
if ($fd) {
$count=0;
while (!feof ($fd) && $count<10) {
$buffer = fgets($fd, 4096);
$field = split(chr(9), trim($buffer));
$field0 = "$field[0]";
$field1 = "$field[1]";

//... do something with $field1 and $field2

$count++;
}
fclose($fd);
}

Stefan

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация