PDA

View Full Version : fgets--not first line



anonymouse
11-07-2005, 05:00 AM
When using fgets, how can you get information from the second or third line, etc, instead of just the first line.
what I've got looks like this:


<?php
{
$myFile = "rate.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);

echo $theData;
}
?>

if that's not possible, can you do it with fread and start after a certain character? For instance fread($fh, 5); gives you characters 1-5, but say I only want 6-10.

Any ideas?

anonymouse
11-07-2005, 06:26 AM
Well, I figured out a way to go from a desired character to another, as such:

<?php
$fh = fopen("rate.txt", "r");

if(!feof($fh))
{
fseek($fh, 20);
echo (fread($fh, 9));
}
fclose($fh);
?>

But then I found a code that claims to be able to go to a specific line, which would be slightly more convenient than counting characters, but I can't really figure out how to get it to work. Here's the code:

<?
function readLine ($linenum,$fh) {
$line = fgets ($fh, 4096);
$pos = -1;
$i = 0;

while (!feof($fh) && $i<($linenum-1)) {
$char = fgetc($fh);
if ($char != "\n" && $char != "\r") {
fseek($fh, $pos, SEEK_SET);
$pos ++;
}
else $i ++;
}
$line = fgets($fh);
return $line;
} //readLine()
?>
With that...how do I run it and where do I specify the line? I tried just running a readLine(2,"rate.txt"); function and variations of this, but it only produces pages of errors. So, how do I apply this? Thanks!

anonymouse
11-11-2005, 08:51 PM
Still no ideas?

mwinter
11-13-2005, 12:10 AM
I didn't test the code you posted, but it seemed a little odd to me. By the way, the second argument was supposed to be a file handle, produced by a successful call to fopen (http://uk.php.net/fopen/), not a file name.

Anyway, this should suit your needs:



function readLine($lineNum, $handle, $length = 1024) {
$line = false;
while($lineNum-- && !feof($handle)) {
$line = fgets($handle, $length);
}
return (-1 !== $lineNum) ? false : $line;
}
It may not necessarily be the most efficient implementation, but that shouldn't matter unless you have a large file. In that instance, I might consider using an index that lists the offset position of each line, or even replacing it all with a database.

The first argument to the function above is desired line, with the first line being 1 (one). The second argument is a file handle, as returned by fopen. The third (optional) argument is the maximum number of characters fgets (http://uk.php.net/fgets/) will read at a time. It defaults to 1024, but if some of your lines are longer, you'll need to pass a more appropriate number.

The fgets function doesn't consider carriage returns (CR; used by Macs) as line separators. However, it handles both line feeds (LF; used by Linux) and CRLF sequences (both; used by Windows) properly. These separators will be included in the returned string so use rtrim (http://uk.php.net/rtrim/) to strip them out, if necessary.

If a problem occurs, like trying to read past the end of a file, the function returns false.

Mike

anonymouse
11-14-2005, 09:08 PM
Thanks, Mike.

I'm really new to php, so this may seem like a very elementary question. I don't really know how to implement functions...so where do I define each of the arguments? Where do I put the line that establishes the lineNum, handle, etc...I know the handle has to be an fopen, but I can't figure out where to put that. If it's easier, maybe you could just show me an example, say, for instance, if I want line 3 (three) of a file "rate.txt"

The only php I have been able to get to work is just simple lines without functions, so it's simply a lack of understanding of functions. I appreciate your help.

mwinter
11-14-2005, 11:19 PM
I don't really know how to implement functions...Is that implement functions, or use them? There's a bit of a difference. :)


so where do I define each of the arguments?That depends on whether you'd want to use them again. You could place the value of each argument in-line with the function call:



myFunction('a string', 32, anotherFunction());
assign the values to variables first:



$arg1 = 'a string';
$arg2 = 32;
$arg3 = anotherFunction();

myFunction($arg1, $arg2, $arg3);
(hopefully using better names), or a mixture of the two as necessary.


Where do I put the line that establishes the lineNum, handle, etc [...]Well, that's up to you (as I said above)...


If it's easier, maybe you could just show me an example, say, for instance, if I want line 3 (three) of a file "rate.txt"...but I'll use variables for this example because it's probably clearer.



$desiredLine = 3;
$filename = 'rate.txt';

/* The @ suppresses any error messages that
* might result from the function call.
*
* As we only want to read from the file,
* it will be opened for reading only.
*/
$fileHandle = @fopen($filename, 'r');

/* However, we can still check if the call
* succeeded because if it failed,
* $fileHandle will evaluate to false.
*/
if(!$fileHandle) {
/* File open operation failed. Do error
* handling here.
*
* Handling mechanisms should be more
* graceful, but for simplicity, we'll
* simply stop execution.
*/
exit();
}

$line = readLine($desiredLine,
$fileHandle);
I hope that helps,
Mike

anonymouse
11-19-2005, 03:02 AM
that's pretty much what I was doing before...just figured I must be doing something wrong, so I've been trying a few things, and it's just not working.
Does the function have to be in the <head> section of the script? The experience I have with functions is only through javascript, in writing and using them. So I understand about variables and such.

I keep getting errors like "Parse error: parse error, unexpected T_FUNCTION", which refers to this line:


function readLine($lineNum, $handle, $length = 1024) {

...this seems to be generating because of the variable $handle, can't figure out why it doesn't like that.

anonymouse
11-19-2005, 03:12 AM
Is that implement functions, or use them? There's a bit of a difference. :)

I guess I mean to call up the function within the script, to echo the line of text.

Twey
11-19-2005, 10:30 AM
this seems to be generating because of the variable $handle, can't figure out why it doesn't like that.I disagree. The parse error means it doesn't expect to find a function definition here. Is there, perhaps, a loop you've forgotten to close?

nightdriver09
12-06-2005, 09:25 AM
Does the function have to be in the <head> section of the script? The experience I have with functions is only through javascript, in writing and using them.

php function declaration could be anywhere between the <?php ?> tags.

you could also do this in your readLine function if youre trying to read a not-so-big file.

function readLine( $desiredLine, $handle, $length=1024 )
{
if ( !$handle )
echo 'not a valid handle.';

$ctr = 0;

while ( $ctr!=$desiredLine )
{
$line = fgets( $handle, $length );
if ( feof($handle) )
{
$line = 'error: invalid line number';
break;
}
$ctr++;
}

echo $line;
}

anonymouse
12-08-2005, 05:53 AM
edit: I finally figured it out! I've been putting the variable definitions in the wrong place, it seems. I just needed to play around with it. Lack of experience. Thanks for your scripts, Mike and Nightdriver. And thanks Twey for pointing that out. Ultimately, that's what helped me see what I was doing wrong.