Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

When using sysseek, it supposedly returns the new file position. But is this the number of bytes _before_ the byte next in line to be read/written to, or is it including the next byte?

For example, I am seeking to the end of the file. To take the number of bytes already in the file (counting from 1!), would I simple take the return value, or would I have to add 1 to it?

Thanks!

  • Comment on What specifically is the "file position"?

Replies are listed 'Best First'.
Re: What specifically is the "file position"?
by blokhead (Monsignor) on Jul 24, 2004 at 00:22 UTC
    The file pointer rests between bytes. The file position returned by tell & friends is the number of bytes behind the pointer. So at the beginning of the file, it's 0, and at the end of the file, it's the file's size. (Think fence posts)

    blokhead

Re: What specifically is the "file position"?
by beable (Friar) on Jul 24, 2004 at 03:00 UTC
    You could try asking the computer:
    #!/usr/bin/perl use strict; use warnings; use Fcntl 'SEEK_END'; my $filename = "testfile"; create_file(); seek_file(); sub create_file { my $size = 4096; open OUTFILE, ">$filename" or die "can't open $filename: $!"; print OUTFILE "x" x $size or die "can't print to $filename: $!"; close OUTFILE or die "can't close $filename: $!"; } sub seek_file { open INFILE, "<$filename" or die "can't open $filename: $!"; my $result = sysseek INFILE, 0, SEEK_END; close INFILE; print "sysseek() returned $result\n"; } __END__ Output: sysseek() returned 4096
Re: What specifically is the "file position"?
by Fletch (Bishop) on Jul 24, 2004 at 00:25 UTC
    What specifically is the "file position"?

    Down your pants leg if you're a former National Security Advisor . . .

    *Bah dah ching*

    Thank you, you're all beautiful people! Don't forget to try the veal! I'm here all week!

      Too bad the Libertarians are the only party that would nominate a software junkie for public office. :)

      (I vaguely remember reading in a party newsletter that their presidential nomination is a programmer from Miami . . . got my vote!) LOL

Re: What specifically is the "file position"?
by tfrayner (Curate) on Jul 25, 2004 at 11:05 UTC
    I was just wondering about thid the other day, similarly because I wanted to seek to the end of a file. It seems to me your original question has been answered, but note that sysseek(FH,0,2)(and seek(FH,0,2)) will seek to the very end of the FH filehandle such that you can then append to it.

    Tim