1
votes

In C, I can use rewind back to the start, but I didn't found a similar way in Rust.

I want to open an existed file, and let the file pointer go back to the start point, write new words to it and cover the old one.

But now I can only write something after the last line of the original file and don't know how to change the file pointer.

I known that rust has a crate libc::rewind, but how to use it, or any other ways?

1

1 Answers

5
votes

Use 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(())
}