fgetpos在Go中可用吗?想要找到File.Position。

huangapple go评论85阅读模式
英文:

fgetpos available in Go? Want to find File.Position

问题

我有一个File,我想找到文件的偏移/位置,在stdio中会是fgetpos。我在http://golang.org/pkg/io/中找不到它。我需要自己计算吗,还是有内置的方法?

英文:

I have a File and I want to find the file offset/position, what would be fgetpos in stdio. I can't seem to find it in http://golang.org/pkg/io/. Do I have to count it myself or is there a build in method?

答案1

得分: 38

你应该能够执行一个<code>Seek()</code>到当前位置的0字节,它会返回结果位置。我不能百分之百确定结果是你想要的绝对位置,但我希望它是这样的。

offset, err := f.Seek(0, io.SeekCurrent)
if err != nil {
    // 处理错误
}
// offset 是当前位置
英文:

You should be able to do a <code>Seek()</code> to 0 bytes from the current position, which returns the resulting position. I'm not 100% sure the result is the absolute position you're after, but I would expect it to be.

offset, err := f.Seek(0, io.SeekCurrent)
if err != nil {
    // handle error
}
// offset is the current position

答案2

得分: 0

这应该可以工作:

currentPosition, err := file.Seek(0, 1)

我从这里阅读到它:

<https://cihanozhan.medium.com/file-operations-in-golang-292825c9fb3d>

英文:

this should work:

currentPosition, err := file.Seek(0, 1)

i read it from here:

<https://cihanozhan.medium.com/file-operations-in-golang-292825c9fb3d>

答案3

得分: -1

如果将其翻译为syscall lseek(),与C ftell()相比,它非常昂贵,并且可能导致昂贵的性能损失。

英文:

If it translates to syscall lseek(), it is quite expensive and can end up with expensive performance penalty compared to C ftell(), for example.

huangapple
  • 本文由 发表于 2012年6月6日 00:43:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/10901351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定