Hello !
I'm working on how to create a file according to a give file name :
main.rs
use std::io;
use std::io::Write;
use std::io::prelude::*;
use std::fs::File;
fn main()
{
print!("Name of the file : ");
io::stdout().flush()
.expect("Error : Failed to flush buffer");
//Getting user input
let mut user_input = String::new();
io::stdin().read_line(&mut user_input)
.expect("Error : Failed to read user input");
//Creating the file
let mut file = File::create(user_input)
.expect("Error : Failed to create the file");
println!("Writing in file {}", user_input);
//Ereasing the content of user_input
user_input = String::from("");
//Getting user input
io::stdin().read_line(&mut user_input)
.expect("Error : Failed to get user input");
//Writing to the file
//Converting input to bytes
file.write_all(user_input.as_bytes())
.expect("Error : Failed to write to the file");
}
Indeed, I would like to create a file according to what the user would like it to be named. But it doesn't work that way, what is the right way ?
Regards