【问题标题】:How to rewind the file pointer in rust如何在rust中倒带文件指针
【发布时间】:2019-09-29 02:12:20
【问题描述】:

在 C 中,我可以使用 rewind 回到开头,但我在 Rust 中没有找到类似的方式。

我想打开一个已经存在的文件,让文件指针回到起点,写新词,覆盖旧的。

但是现在只能在原文件最后一行后面写点东西,不知道怎么改文件指针。

我知道rust有一个crate libc::rewind,但是如何使用它,或者有什么其他方法呢?

【问题讨论】:

    标签: rust


    【解决方案1】:

    使用seek

    use std::io::{self, Seek, SeekFrom};
    use std::fs::File;
    
    fn main() -> io::Result<()> {
        let mut file = File::open("foo.bar")?;
        file.seek(SeekFrom::Start(0))?;
        Ok(())
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 2018-05-26
      相关资源
      最近更新 更多