can't open file if its name was read from stdin

231 views
Skip to first unread message

buzze...@gmail.com

unread,
Aug 24, 2017, 11:35:31 PM8/24/17
to golang-nuts
Does anyone know why Google Go program 1 works, below, and program 2 doesn't? The only difference - I read in the filename from the console in the one that doesn't work but it is a literal in the one that works... I tried typing in the full path for the file, and also "..sample.txt" instead of "sample.txt", but they didn't solve it either.
// program 1 - this works:

    package main import (
            "fmt"
            "os"
            "bufio" )
        
        func main() {
            filename := getFilename()
        fmt.Println("opening:",filename) 
    //      fmt.Println("opening:sample.txt")
            _ , err := os.Open("sample.txt") 
    //   _ , err := os.Open(filename)
            if err != nil { 
        panic(err)
            } 
        } 

    func getFilename()  string {
        reader := bufio.NewReader(os.Stdin)
        fmt.Print("Enter file name: ")    // type in anything, this file name not used
        filename, _ := reader.ReadString('\n')
        return filename
    }


// program 2 - this doesn't work, gives error shown below:

    package main
    import (
        "fmt"
        "os"
        "bufio"
    )
    
    func main() 
        filename := getFilename()
        fmt.Println("opening:",filename)
    // fmt.Println("opening: sample.txt")
    // _ , err := os.Open("sample.txt")
        _ , err := os.Open(filename)
        if err != nil {
        panic(err)
        }
        }

    func getFilename()  string {
        reader := bufio.NewReader(os.Stdin)
        fmt.Print("Enter file name: ")    // type in "sample.txt" (without the quotes)
        filename, _ := reader.ReadString('\n')
        return filename
    }

Error message, panic invoked - 
: The filename, directory name, or volume label syntax is incorrect.

goroutine 1 [running]:
main.main()
        C:/installed programs/go/src/testopen2.go:16 +0x158
exit status 2

Dave Cheney

unread,
Aug 25, 2017, 12:30:51 AM8/25/17
to golang-nuts
Check the error from ReadString to find out why it's returning a empty string.

Uli Kunitz

unread,
Aug 25, 2017, 1:03:01 AM8/25/17
to golang-nuts
ReadString returns the line including the terminating newline ('\n'). You can check it with fmt.Printf("filename: %q\n", filename). One option to fix it is using function strings.TrimSpace. It will remove all space characters at the start and the end of the string. 

Geoff Fridd

unread,
Aug 25, 2017, 4:52:23 PM8/25/17
to golang-nuts
I found the solution myself - when read in from stdin, there are 2 extra characters - CR and LF.  I just cut the string 2 characters before the end, and it works!  Thanks to those for posting replies!

Uli Kunitz

unread,
Aug 26, 2017, 12:30:24 AM8/26/17
to golang-nuts
Good, that you found the problem yourself. I wasn't aware that you were on Windows were newlines are represented by CRLF ("\r\n"). TrimSpace would deal with that as well.
Reply all
Reply to author
Forward
0 new messages