dousong1926 2017-06-27 04:55
浏览 179
已采纳

golang不能将bufio.ReadString {}与ioutil.ReadFile()一起使用

If I try to read the file name through the terminal go can't seem to find it. But if I hard code it everything works out fine? this isn't a problem with writing out.

This code:

package main

import (
"bufio"
"fmt"
"io/ioutil"
"os")

func check(e error) {
    if e != nil {
        panic(e)
    }
}

func getUserInput(message string) (text string){
    reader := bufio.NewReader(os.Stdin)
    fmt.Println(message)
    text, err := reader.ReadString('
')
    check(err)
    return text
}

func main() {
    input := getUserInput("File to open?")
    fmt.Println(input)
    dat, err := ioutil.ReadFile(input)
    check(err)

    fmt.Print("% x 
", dat)
    input = getUserInput("File to write?")
    d1 := []byte(dat)
    e := ioutil.WriteFile(input, d1, 0644)
    check(e)
}

Yields:

panic: open a.jpg
: no such file or directory

goroutine 1 [running]:
runtime.panic(0x4a36c0, 0xc21001d2a0)
    /usr/lib/go/src/pkg/runtime/panic.c:266 +0xb6
main.check(0x7fdcd2e07088, 0xc21001d2a0)
    /home/matt/Dropbox/CSE3320/fs_GO/fs.go:17 +0x4f
main.main()
    /home/matt/Dropbox/CSE3320/fs_GO/fs.go:35 +0x13e
exit status 2

yet this code:

package main

import (
"bufio"
"fmt"
"io/ioutil"
"os")

func check(e error) {
    if e != nil {
        panic(e)
    }
}

func getUserInput(message string) (text string){
    reader := bufio.NewReader(os.Stdin)
    fmt.Println(message)
    text, err := reader.ReadString('
')
    check(err)
    return text
}

func main() {
        //input := getUserInput("File to open?")
        //fmt.Println(input)
    dat, err := ioutil.ReadFile("a.jpg")
    check(err)

    //fmt.Print("% x 
", dat)
    input := getUserInput("File to write?")
    d1 := []byte(dat)
    e := ioutil.WriteFile(input, d1, 0644)
    check(e)
}

Appears to work just fine

When running the first code without checking for error a blank new file is created. Yet the second run works perfectly fine even writing the file with the same function to get the new file name.

I'm completely lost as to what I'm doing wrong here. Here is my version info if that helps let me know what else you need.

go version gives me go version go1.2.1 linux/amd64.

  • 写回答

1条回答 默认 最新

  • dourong6054 2017-06-27 05:01
    关注

    The problem is that the filename ends in a newline:

    panic: open a.jpg
    : no such file or directory
    

    That should be:

    panic: open a.jpg: no such file or directory
    

    This is because ReadString() (emphasis mine):

    reads until the first occurrence of delim in the input, returning a string containing the data up to and including the delimiter.

    Use input = strings.TrimSpace(input) to remove the newline.


    Extra tip 1: Instead of using fmt.Println(input) to debug stuff, you should use fmt.Printf("%#v ", input). This will show whitespace and unprintable characters clearer.

    Extra tip 2: Go 1.2.1 is quite old; you should consider using a more recent version. Go is very compatible and upgrading to a newer version is usually not a problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题