英文:
program only prints last string of input file
问题
我正在尝试创建一个简单的程序,用于从文本文件中读取行并将它们打印到控制台中。我花了很多时间检查我的代码,但我无法理解为什么只有最后一行被打印到屏幕上。有人可以告诉我我在这里做错了什么吗?这里的所有内容都应该可以编译和运行。
package main
import (
"bufio"
"fmt"
"os"
)
func Readln(r *bufio.Reader) (string, error) {
var (
isPrefix bool = true
err error = nil
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
return string(ln), err
}
func main() {
f, err := os.Open("tickers.txt")
if err != nil {
fmt.Printf("打开文件时出错:%v\n", err)
os.Exit(1)
}
r := bufio.NewReader(f)
s, e := Readln(r)
for e == nil {
fmt.Println(s)
s, e = Readln(r)
}
}
这段代码的问题在于,你只调用了一次Readln
函数来读取文件的第一行,然后在一个循环中重复打印这一行。你需要在循环中继续调用Readln
函数来读取文件的下一行。以下是修改后的代码:
package main
import (
"bufio"
"fmt"
"os"
)
func Readln(r *bufio.Reader) (string, error) {
var (
isPrefix bool = true
err error = nil
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
return string(ln), err
}
func main() {
f, err := os.Open("tickers.txt")
if err != nil {
fmt.Printf("打开文件时出错:%v\n", err)
os.Exit(1)
}
r := bufio.NewReader(f)
for {
s, e := Readln(r)
if e != nil {
break
}
fmt.Println(s)
}
}
这样修改后,程序将会循环读取文件的每一行并将其打印到控制台上。
英文:
i am trying to create a simple program to read lines from a text file and print them out to the console in golang. I spent lots of time going over my code and I simply can't understand why only the last line is being printed out to the screen. can anyone tell me where I am going wrong here? Everything here should compile and run.
package main
import (
"bufio"
"fmt"
"os"
)
func Readln(r *bufio.Reader) (string, error) {
var (
isPrefix bool = true
err error = nil
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
return string(ln), err
}
func main() {
f, err := os.Open("tickers.txt")
if err != nil {
fmt.Printf("error opening file: %v\n", err)
os.Exit(1)
}
r := bufio.NewReader(f)
s, e := Readln(r)
for e == nil {
fmt.Println(s)
s, e = Readln(r)
}
}
答案1
得分: 1
因此,我怀疑问题出在你的tickers.txt
文件的行尾符上。ReadLine()的文档还指出,在大多数情况下,Scanner更适合使用。
以下的Stack Overflow问题提供了一些有关替代实现的有用信息:https://stackoverflow.com/questions/8757389/reading-file-line-by-line-in-go
然后,我使用上述问题中的示例来重新实现你的main函数,代码如下:
f, err := os.Open("tickers.txt")
if err != nil {
fmt.Printf("打开文件时出错:%v\n", err)
os.Exit(1)
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
}
英文:
I therefore suspect that the problem is in your tickers.txt
file line endings. The docs for ReadLine() also indicate that for most situations a Scanner is more suitable.
The following SO question has some useful information for alternative implementations: https://stackoverflow.com/questions/8757389/reading-file-line-by-line-in-go
I then used the example in the above question to re-implement your main function as follows:
f, err := os.Open("tickers.txt")
if err != nil {
fmt.Printf("error opening file: %v\n", err)
os.Exit(1)
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论