Code Monkey home page Code Monkey logo

Comments (2)

burmecia avatar burmecia commented on June 19, 2024

@gameldar thank you very much for this issue.

This is an expected result. What happened is that the 2nd short file write was overwriting the file on top of previous version.

For example, suppose we have a long buffer [1, 2, 3, 4, 5] and a short buffer [6, 7, 8]. If we write the long buffer we got the 1st version of content:

version 1: [1, 2, 3, 4, 5]

Then we write the short buffer, we are actually writing on top of the version 1 file from the beginning, not the original empty file, so we got:

version 2: [6, 7, 8, 4, 5]

That's what we're expecting. It is exactly the same behavior if you use std::fs to write a file.

fn main() {
    let buf = [1, 2, 3, 4, 5];
    let buf2 = [6, 7, 8];

    let mut f = std::fs::OpenOptions::new()
        .write(true)
        .create(true)
        .open("tt.bin")
        .unwrap();
    f.write_all(&buf).unwrap();

    let mut f = std::fs::OpenOptions::new()
        .write(true)
        .create(true)
        .open("tt.bin")
        .unwrap();
    f.write_all(&buf2).unwrap();
 }

The above code will produce the file:

$ hexdump ./tt.bin
0000000 06 07 08 04 05

If you want to write a fresh content to the file, you can consider add truncate option, which will clear the content before writing.

// at the 2nd short file write
let mut f = OpenOptions::new()
        .create(true)
        .truncate(true)
        .open(&mut repo, "/myfile.txt")?;

from zbox.

gameldar avatar gameldar commented on June 19, 2024

Ahh yes I see now. Most of the time in the past (in other languages) I've inherently passed through the truncate flag without realising it (e.g. it is the default behaviour).

But the implementation here is exactly what I want to use in another use case as well and using the truncate solves my immediate issue.

Thanks for the quick reply!

from zbox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.