英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论