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.

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

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码