0

I am trying to take input from the user in the form of a String and passing it as a path for Rodio to play the audio file. When I pass it a hard-coded path it seems to work just fine but when I type the same path in as input it will give me an error.

code:

fn main() {

    let mut path = String::new();
    io::stdin().read_line(&mut path); //to get input from the user

    player(&path);
}
fn player(path: &str){
    let (stream, stream_handle) = rodio::OutputStream::try_default().unwrap();

    // Load a sound from a file, using a path relative to Cargo.toml
    let file = File::open(path).unwrap();
    let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
    stream_handle.play_raw(source.convert_samples());

    // The sound plays in a separate audio thread,
    // so we need to keep the main thread alive while it's playing.
    loop {

    }
}

Input: C:\Users\username\AppData\Roaming\Equinotify\songs\Olivia Rodrigo - good 4 u (Official Video).wav

Input: C:\\Users\\Drago\\AppData\\Roaming\\Equinotify\\songs\\Olivia Rodrigo - good 4 u (Official Video).wav

Neither input works in this scenario and gives this error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 123, kind: Other, message: "The filename, directory name, or volume label syntax is incorrect." }', src\main.rs:24:33

However, when I hardcode the exact same input it works just fine. Your help is greatly appreciated!

Equinox
  • 120
  • 2
  • 8
  • I suggest doing a test in your code: Add the hardcoded path, then do a comparison to the user-input path, and see if they are _exactly_ the same. Might be some problems with character encoding, escape sequences, stuff like that? Also, I think for paths there are better classes than `String.` Like `std::path::Path` – cadolphs Jun 23 '21 at 21:00
  • The two look identical unless I am missing something, I did also change out the strings for path variables and it didn't fix the problem. – Equinox Jun 23 '21 at 21:43
  • But have you verified, programatically, that they are identical? Looks can be deceiving. – cadolphs Jun 23 '21 at 22:00
  • Your correct they are not the same, what can I do to fix this? – Equinox Jun 23 '21 at 22:11
  • Well, in what way are they not the same? Extra slashes or stuff like that? – cadolphs Jun 23 '21 at 22:12
  • They look exactly the same but when tested with an if statement to test whether they were the same or not it failed. This could also be because of how rust checks its conditionals but I think when directly comparing to a string that shouldn't be a problem. So while they look the same to me when I have them printed out they are apparently not. – Equinox Jun 23 '21 at 22:21
  • When you read a line from stdin, it typically comes with the newline included at the end (from when you pressed the enter key). If you print out the strings using the debug format specifier, i.e. `println!("{:?}", &path);`, it will show any escape sequences in the string you could not otherwise see. You may need to use [`str::trim`](https://doc.rust-lang.org/stable/std/primitive.str.html#method.trim) or a similar method to remove the newline. – Mac O'Brien Jun 24 '21 at 00:07
  • Yep, I actually fixed it using a different crate called read_input it adds some extra stuff to rust's read line which is kinda bad to begin with because you can't just set a variable to user input. Anyways thanks for the help. – Equinox Jun 24 '21 at 00:25
  • Does this answer your question? [Why does my string not match when reading user input from stdin?](https://stackoverflow.com/questions/27773313/why-does-my-string-not-match-when-reading-user-input-from-stdin) – Jmb Jun 24 '21 at 06:31

1 Answers1

1

When you read a line from stdin, it typically comes with the new line included at the end (from when you pressed the enter key).

If you print out the strings using the debug format specifier, i.e. println!("{:?}", &path);, it will show any escape sequences in the string you could not otherwise see.

You may need to use str::trim or a similar method to remove the newline. – Cormac O'Brien

Equinox
  • 120
  • 2
  • 8