File.Tell

2,203 views
Skip to first unread message

gwenn

unread,
Mar 10, 2012, 9:17:28 AM3/10/12
to golang-nuts
Hello,
I am looking for the equivalent of ftell in Go.
For fseek, it's os.File.Seek.
But for ftell, I've found nothing.
Could you please help me?
Thanks.

Brad Fitzpatrick

unread,
Mar 10, 2012, 12:42:56 PM3/10/12
to gwenn, golang-nuts
offset, err := file.Seek(os.SEEK_CUR, 0)

Scott Lawrence

unread,
Mar 10, 2012, 12:43:41 PM3/10/12
to gwenn, golang-nuts
According to the docs as tip.golang.org, for File.Seek:

Seek sets the offset for the next Read or Write on file to offset,
interpreted according to whence: 0 means relative to the origin of the file,
1 means relative to the current offset, and 2 means relative to the end. It
returns the new offset and an error, if any.

So, I think f.Seek(0, 1) should give you what you're looking for.

--
Scott Lawrence

go version weekly.2012-02-22 +a2275531cfe0
Linux jagadai 3.2.8-1-ARCH #1 SMP PREEMPT Mon Feb 27 21:51:46 CET 2012 x86_64 Intel(R) Core(TM)2 Duo CPU P8700 @ 2.53GHz GenuineIntel GNU/Linux

Stephen Weinberg

unread,
Mar 10, 2012, 12:44:33 PM3/10/12
to golang-nuts
Seek gives your current position when you use it. So if you seek zero
ahead, Seek() returns your current position.

pos, err := f.Seek(0, os.SEEK_CUR)

-- Stephen

Brad Fitzpatrick

unread,
Mar 10, 2012, 12:58:28 PM3/10/12
to gwenn, golang-nuts
On Sat, Mar 10, 2012 at 9:42 AM, Brad Fitzpatrick <brad...@golang.org> wrote:
offset, err := file.Seek(os.SEEK_CUR, 0)

er, whoops, backwards.

gwenn

unread,
Mar 10, 2012, 12:45:33 PM3/10/12
to Scott Lawrence, golang-nuts
Yes,
File.Seek(0, os.SEEK_CUR) does the job.
Many thanks.
Reply all
Reply to author
Forward
0 new messages