I have a csv file with sorting entries in the first field . I managed to create a function that performs a binary search through this file, using fseek for random access through the file.
However, this is still a rather slow process, since when I search for some position in the file, I really need to look left while looking at \ n characted, so I can make sure that I read the whole line (after the whole line being read, I can check the value of the first field mentioned above).
Here is a function that returns a string containing the character at position x:
function fgetLineContaining( $fh, $x ) {
if( $x 125145411)
return "";
while( $x > 0 && $c != "\n" && $c != "\r") {
fseek($fh, $x);
$x--;
$c = fgetc( $fh );
}
$x+=2;
fseek( $fh, $x );
return fgets( $fh, 1024 );
}
While this works as expected, I have to be sad that my csv file has ~ 1.5Mil lines, and these left-handed distortions slow down to a large extent.
, x ?
, , , . php ?