How to use &mut File.set_len(), if OpenOptions::append(true)

my code:

 match File::create_new(&fp) {
            Ok(f) => Ok(f),
            Err(x) => match x {
                y if y.kind() == ErrorKind::AlreadyExists => {
                    File::options().read(true).append(true).open(&fp)
                }

                other => Err(other),
            },
        }
    pub fn format(&self) -> io::Result<()> {
        let mut data = Vec::new();
        let mut fl = self.file.borrow_mut(); 
        fl.read.rewind()?;
        fl.read.read_from_file_until(&mut data, 4000)?;
        let mut data = String::from_utf8_lossy(&data).into_owned();
        let ind = data
            .match_indices(".")
            .map(|x| {
                x.0
            })
            .collect::<Vec<usize>>();
        Self::_add_char(&mut data, &ind, '\n');
        println!("{} {:?}", data, ind);
       dbg!(fl.write.get_mut().set_len(0))?;
        dbg!(fl.write.rewind())?;
        dbg!(fl.write.get_mut().write_all(data.as_bytes()))?;
        Ok(())
    }

error:

fl.write.get_mut().set_len(0)=Err(
    Os {
        code: 5,
        kind: PermissionDenied,
        message: "...",
    },
)

Does the file you use for testing already exist or not? create_new does not set the append option to true, maybe that is the reason why an error is given?

Yes, this file already exists

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.