0

I am trying to create file using keyboard input, but the created file's name has unnecessary symbols.

use std::io;
use std::fs::File;

fn main() {
    let mut filesname = String::new();
    io::stdin().read_line(&mut filesname)
        .ok()
        .expect("cant read string");
    filecreate(&filesname);
}

fn filecreate(path: &str) {
    let f = File::create(path);
    println!("Ok, file {} was created.", path);
}

Using this code I wrote "foo.txt", but my file manager shows this file as "'foo.txt'$'\n'". I can't figure out what I have to do with the user's input.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
faras234
  • 19
  • 1
  • 4
    `read_line()` also reads the newline; see [this question](http://stackoverflow.com/questions/39725783/rust-not-properly-reading-integer-input) for the same issue. – ljedrz Sep 28 '16 at 07:36
  • The only extra symbol is `\n` which is printed by `ls` as `$'\n'`. In bash, newlines can be represented inside single quotes if the single quotes are preceded by `$`, and strings can be concatenated by simply placing them beside each other. That is, `$'foo\n'` == `'foo'$'\n'` == `'fo'$'o\n'` == `'f''o''o'$'\n'` == `'f'oo$'\n'`. So just trim the final newline off! – John Sep 28 '16 at 10:39

0 Answers0