不能将bufio.ReadString {}与ioutil.ReadFile()一起使用 [英] Can't use bufio.ReadString{} with ioutil.ReadFile()

查看:64
本文介绍了不能将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.

此代码:

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('\n')
    check(err)
    return text
}

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

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

收益:

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

尚此代码:

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('\n')
    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 \n", dat)
    input := getUserInput("File to write?")
    d1 := []byte(dat)
    e := ioutil.WriteFile(input, d1, 0644)
    check(e)
}

看起来工作正常

在运行第一个代码而不检查错误时,将创建一个空白的新文件.即使使用相同的功能写入文件以获取新的文件名,第二次运行也可以正常工作.

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 给了我 go version go1.2.1 linux/amd64 .

推荐答案

问题在于文件名以换行符结尾:

The problem is that the filename ends in a newline:

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

应该是:

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

这是因为 ReadString() (强调我的):

This is because ReadString() (emphasis mine):

读取直到输入中第一次出现delim为止,并返回一个字符串,其中包含直至和包括定界符的数据.

使用 input = strings.TrimSpace(input)删除换行符.

额外提示1:您应该使用 fmt.Printf(%#v \ n",input) fmt.Println(input)调试内容>.这样可以更清晰地显示空格和不可打印的字符.

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

额外提示2:Go 1.2.1相当老.您应该考虑使用较新的版本. Go非常兼容,通常升级到新版本不是问题.

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.

这篇关于不能将bufio.ReadString {}与ioutil.ReadFile()一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆