Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

What specifically is the "file position"?

by Anonymous Monk
on Jul 23, 2004 at 23:51 UTC ( [id://377037]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://377037]
Approved by ysth
Front-paged by Anneq
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-25 06:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found