英文:
Can't use bufio.ReadString{} with ioutil.ReadFile()
问题
如果我尝试通过终端读取文件名,Go似乎找不到它。但是如果我硬编码它,一切都正常?这不是写出的问题。
这段代码:
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.Printf("% 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.Printf("% x \n", dat)
input := getUserInput("File to write?")
d1 := []byte(dat)
e := ioutil.WriteFile(input, d1, 0644)
check(e)
}
似乎完全正常工作。
在运行第一个代码时,如果不检查错误,会创建一个空的新文件。然而,第二次运行完全正常,甚至使用相同的函数来获取新文件名并写入文件。
我完全不知道我在这里做错了什么。如果有帮助,请告诉我您需要什么其他信息。
go version
给出 go version go1.2.1 linux/amd64
。
英文:
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
gives me go version go1.2.1 linux/amd64
.
答案1
得分: 6
问题在于文件名以换行符结尾:
panic: open a.jpg
: no such file or directory
应该是:
panic: open a.jpg: no such file or directory
这是因为ReadString()
(我强调):
> 从输入中读取,直到第一次出现分隔符为止,返回一个包含数据和分隔符的字符串。
使用input = strings.TrimSpace(input)
来去除换行符。
额外提示1:调试代码时,不要使用fmt.Println(input)
,而应该使用fmt.Printf("%#v\n", input)
。这样可以更清楚地显示空格和不可打印字符。
额外提示2:Go 1.2.1版本相当旧了,你应该考虑使用更新的版本。Go非常兼容,升级到新版本通常不会有问题。
英文:
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\n", 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论