Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append to text file #23

Open
alanvardy opened this issue Dec 29, 2022 · 0 comments
Open

Append to text file #23

alanvardy opened this issue Dec 29, 2022 · 0 comments

Comments

@alanvardy
Copy link
Owner

To append to a text file in Rust, you can use the fs::OpenOptions::append method. This method takes a path to the file and returns a Result<File, io::Error> object. You can then use the write_all method on the File object to write data to the file.

Example:

use std::fs::OpenOptions;

fn main() {
    let mut file = OpenOptions::new()
        .write(true)
        .append(true)
        .open("my_file.txt")
        .expect("Failed to open file");

    file.write_all(b"This is some data to append to the file")
        .expect("Failed to write to file");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant