Jump to content

As the title implies, I'm working on trying to read input from a user (The file name) then use that input to open the file. For some reason if i put the file name in manually "myfile.txt" it will open but when using the variable it is stored to userinput it will not open 

 

reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter text: ")
    userinput, _ := reader.ReadString('\n')
    fmt.Println(userinput)
 
file, err := os.Open("myfile.txt")       //need to get this to take a userinput (Currently gives an error when trying os.Open(userinput)) 
        if err != nil {
            log.Fatal(err)
        }
        defer file.Close()
 
        scanner := bufio.NewScanner(file)
 
        for scanner.Scan() {
            fmt.Println(scanner.Text()) //need to get this into an array seperated by spaces (Currently reads line by line)
        }
 
this is currently what I have, os.Open is the preferred way id like to do this (mostly because i already have the scanner set up) but I'm more than happy to change code if anyone can help me with this issue.
Thanks
Link to comment
https://linustechtips.com/topic/942562-using-user-input-to-open-file/
Share on other sites

Link to post
Share on other sites

The user input will have a new line ('\n') at the end, you need to remove that before you use it.

 

Try:

reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter text: ")
    userinput, _ := reader.ReadString('\n')
    userinput = strings.TrimSuffix(userinput, "\n")
    fmt.Println(userinput)

 

 

More info: https://flaviocopes.com/golang-remove-new-line-from-readstring/

Link to post
Share on other sites

8 hours ago, Meic said:

The user input will have a new line ('\n') at the end, you need to remove that before you use it.

 

Try:


reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter text: ")
    userinput, _ := reader.ReadString('\n')
    userinput = strings.TrimSuffix(userinput, "\n")
    fmt.Println(userinput)

 

 

More info: https://flaviocopes.com/golang-remove-new-line-from-readstring/

Ah, this makes sense now, I'll try this and hopefully this should fix the problem thanks.

Link to post
Share on other sites

 

On 6/29/2018 at 4:42 AM, HeaterUp said:

As the title implies, I'm working on trying to read input from a user (The file name) then use that input to open the file. For some reason if i put the file name in manually "myfile.txt" it will open but when using the variable it is stored to userinput it will not open

Just an fyi to help make things easier for you in the future, but I would put the language you're using in the title of your ticket, preferably at the beginning and in parentheses or square brackets to differentiate it from the rest of the text.  That way it'll stand out more to people with experience in that language.

 

I'm happy that Meic was able to help you, and I hope that solved your problem.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×