fseek

  • PHP

    $f = fopen('/path/to/foobar.txt', 'r');
    fseek($f, 3);
    // do something at position 3
    fclose($f);

    Ruby

    File.open('/path/to/foobar.txt', 'r') do |f|
      f.seek(3)
      # do something at position 3
    end

    Both PHP’s fseek and Ruby’s File#seek support an optional third argument that specifies the seek behavior. This argument takes constants that are virtually identical in both name and behavior between PHP and Ruby.

    PHP Ruby
    SEEK_SET IO::SEEK_SET
    SEEK_CUR IO::SEET_CUR
    SEEK_END IO::SEEK_END

    If you are doing an absolute seek (IO::SEEK_SET) you might find it more convenient to use File#pos=.

    Ruby

    # all three are equivalent
    f.seek(3)
    f.seek(3, IO::SEEK_SET)
    f.pos = 3

    see also

fread fstat

Looking for Rails or PHP web application development, integration, and training?

Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.