gpt4 book ai didi

file - 如何获取当前光标在文件中的位置?

转载 作者:行者123 更新时间:2023-11-29 07:52:30 26 4
gpt4 key购买 nike

给定这段代码:

let any_offset: u64 = 42;
let mut file = File::open("/home/user/file").unwrap();
file.seek(SeekFrom::Start(any_offset));
// println!("{:?}", file.cursor_position())

如何获取当前光标位置?

最佳答案

你应该调用 Seek:seek 相对偏移量为 0。这没有副作用并会返回您要查找的信息。

Seek为多种类型实现,包括:

  • impl Seek for File
  • impl<'_> Seek for &'_ File
  • impl<'_, S: Seek + ?Sized> Seek for &'_ mut S
  • impl<R: Seek> Seek for BufReader<R>
  • impl<S: Seek + ?Sized> Seek for Box<S>
  • impl<T> Seek for Cursor<T> where
  • impl<W: Write + Seek> Seek for BufWriter<W>

使用 Cursor mentioned by Aaronepower不过可能效率更高,因为您可以避免进行额外的系统调用。

关于file - 如何获取当前光标在文件中的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34878970/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com