ADVERTISEMENT
ADVERTISEMENT

Rust program to append data into an existing file

Rust | File I/O Example: Write a program to append data into an existing file.
Submitted by Nidhi, on October 30, 2021

Problem Solution:

In this program, we will open an existing file in append mode and write text data into the file using the write_all() method, and print the appropriate message.

Program/Source Code:

The source code to append data into the existing file is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.

// Rust program to append data into existing file

use std::fs::OpenOptions;
use std::io::Write;

fn main() {
    let mut fileRef = OpenOptions::new().append(true).open("sample.txt").expect("Unable to open file");   
    
    fileRef.write_all("www.includehelp.com\n".as_bytes()).expect("write failed");
    println!("Data appended successfully");
}

Output:

$ rustc main.rs

$ ./main
Data appended successfully

$ cat sample.txt 
Hello World
Hello India
www.includehelp.com

Explanation:

In the main() function, we opened an existing file "sample.txt" in append mode. Then we wrote text data into the file using the write_all() method and printed file data.

Rust File I/O Programs »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.