Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: fgets--not first line

  1. #1
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default fgets--not first line

    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:
    Code:
    <?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?

  2. #2
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well, I figured out a way to go from a desired character to another, as such:
    Code:
    <?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:
    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!

  3. #3
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Still no ideas?

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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, not a file name.

    Anyway, this should suit your needs:

    PHP Code:
    function readLine($lineNum$handle$length 1024) {
      
    $line false;
      while(
    $lineNum-- && !feof($handle)) {
        
    $line fgets($handle$length);
      }
      return (-
    !== $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 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 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

  5. #5
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  6. #6
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by anonymouse
    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:

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

    PHP Code:
    $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.

    PHP Code:
    $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

  7. #7
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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:
    PHP Code:
    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.

  8. #8
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by mwinter
    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.

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Dec 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Quote Originally Posted by anonymouse
    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;
    }
    Last edited by nightdriver09; 12-06-2005 at 09:50 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •