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

查看:502
本文介绍了golang不能与ioutil.ReadFile()一起使用bufio.ReadString {}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试通过终端读取文件名称,似乎无法找到它。但如果我硬编码它一切正常?这不是写出来的问题。

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('\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)
}

收益率:

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('\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版本go1.2.1 linux / amd64

推荐答案

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

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

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

This is because ReadString() (emphasis mine):


会一直读取,直到输入中第一次出现delim,返回一个包含数据的字符串,并且包括分隔符

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

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

额外提示1:取而代之使用 fmt.Println(input)来调试东西时,应该使用 fmt.Printf(%#v \ n,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.

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

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