0

When I use File::open(), I get the following error:

The filename, directory name, or volume label syntax is incorrect. (os error 123)

This is my code:

println!("enter an input file:");
match io::stdin().read_line(&mut input) {
    Err(_) => {
        println!("input error");
        return;
    }
    _ => {}
}

let pinput = Path::new(&input);

match File::open(&pinput) {
    Ok(mut finput) => {
        // ...
    }
    Err(e) => println!("input file error: {}, {}", e, pinput.display()),
}

The file I am trying to open is C:/Users/Name/test.txt where Name is my name, the path contains no special characters or spaces, just members of the English alphabet.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
YourGamerMom
  • 377
  • 3
  • 13
  • Doesn't Windows use backslash `\ ` for path separator? – trent Sep 15 '16 at 20:33
  • @trentcl Windows itself translates both types of slashes. – Shepmaster Sep 15 '16 at 20:39
  • @trentcl nope, it can handle either in a pretty flexible and user friendly way. [Rust on Windows apparently can, too](https://github.com/rust-lang/rust/issues/2752). YourGamerMom - so run [SysInternals ProcMon](https://technet.microsoft.com/en-us/sysinternals/processmonitor.aspx) while you try to open the file, and see what path it's actually reaching for, and what happens. – TessellatingHeckler Sep 15 '16 at 20:39
  • The file you are trying to open is `"C:/Users/Name/test.txt\n"`. `read_line` includes the newline from hitting **Enter**. You can see this by printing out the value of `input` with the `Debug` formatter. Also, you can just say `File::open(&input)` and you should check out `if let`. – Shepmaster Sep 15 '16 at 20:43
  • @Shepmaster that might just be it! thanks. I assume rust's `String` type has some `.strip()` functionality, that might resolve the issue. – YourGamerMom Sep 15 '16 at 21:14
  • @YourGamerMom Yep; check the linked duplicate for the method name. – Shepmaster Sep 15 '16 at 21:19
  • I knew that both work in Windows Explorer, but I didn't know the API did translation. Good to know. – trent Sep 15 '16 at 21:54

0 Answers0