程序只打印输入文件的最后一个字符串。

huangapple go评论108阅读模式
英文:

program only prints last string of input file

问题

我正在尝试创建一个简单的程序,用于从文本文件中读取行并将它们打印到控制台中。我花了很多时间检查我的代码,但我无法理解为什么只有最后一行被打印到屏幕上。有人可以告诉我我在这里做错了什么吗?这里的所有内容都应该可以编译和运行。

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. func Readln(r *bufio.Reader) (string, error) {
  8. var (
  9. isPrefix bool = true
  10. err error = nil
  11. line, ln []byte
  12. )
  13. for isPrefix && err == nil {
  14. line, isPrefix, err = r.ReadLine()
  15. ln = append(ln, line...)
  16. }
  17. return string(ln), err
  18. }
  19. func main() {
  20. f, err := os.Open("tickers.txt")
  21. if err != nil {
  22. fmt.Printf("打开文件时出错:%v\n", err)
  23. os.Exit(1)
  24. }
  25. r := bufio.NewReader(f)
  26. s, e := Readln(r)
  27. for e == nil {
  28. fmt.Println(s)
  29. s, e = Readln(r)
  30. }
  31. }

这段代码的问题在于,你只调用了一次Readln函数来读取文件的第一行,然后在一个循环中重复打印这一行。你需要在循环中继续调用Readln函数来读取文件的下一行。以下是修改后的代码:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. func Readln(r *bufio.Reader) (string, error) {
  8. var (
  9. isPrefix bool = true
  10. err error = nil
  11. line, ln []byte
  12. )
  13. for isPrefix && err == nil {
  14. line, isPrefix, err = r.ReadLine()
  15. ln = append(ln, line...)
  16. }
  17. return string(ln), err
  18. }
  19. func main() {
  20. f, err := os.Open("tickers.txt")
  21. if err != nil {
  22. fmt.Printf("打开文件时出错:%v\n", err)
  23. os.Exit(1)
  24. }
  25. r := bufio.NewReader(f)
  26. for {
  27. s, e := Readln(r)
  28. if e != nil {
  29. break
  30. }
  31. fmt.Println(s)
  32. }
  33. }

这样修改后,程序将会循环读取文件的每一行并将其打印到控制台上。

英文:

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.

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. func Readln(r *bufio.Reader) (string, error) {
  8. var (
  9. isPrefix bool = true
  10. err error = nil
  11. line, ln []byte
  12. )
  13. for isPrefix && err == nil {
  14. line, isPrefix, err = r.ReadLine()
  15. ln = append(ln, line...)
  16. }
  17. return string(ln), err
  18. }
  19. func main() {
  20. f, err := os.Open("tickers.txt")
  21. if err != nil {
  22. fmt.Printf("error opening file: %v\n", err)
  23. os.Exit(1)
  24. }
  25. r := bufio.NewReader(f)
  26. s, e := Readln(r)
  27. for e == nil {
  28. fmt.Println(s)
  29. s, e = Readln(r)
  30. }
  31. }

答案1

得分: 1

因此,我怀疑问题出在你的tickers.txt文件的行尾符上。ReadLine()的文档还指出,在大多数情况下,Scanner更适合使用。

以下的Stack Overflow问题提供了一些有关替代实现的有用信息:https://stackoverflow.com/questions/8757389/reading-file-line-by-line-in-go

然后,我使用上述问题中的示例来重新实现你的main函数,代码如下:

  1. f, err := os.Open("tickers.txt")
  2. if err != nil {
  3. fmt.Printf("打开文件时出错:%v\n", err)
  4. os.Exit(1)
  5. }
  6. scanner := bufio.NewScanner(f)
  7. for scanner.Scan() {
  8. fmt.Println(scanner.Text())
  9. }
  10. if err := scanner.Err(); err != nil {
  11. fmt.Println(err)
  12. }
英文:

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:

  1. f, err := os.Open("tickers.txt")
  2. if err != nil {
  3. fmt.Printf("error opening file: %v\n", err)
  4. os.Exit(1)
  5. }
  6. scanner := bufio.NewScanner(f)
  7. for scanner.Scan() {
  8. fmt.Println(scanner.Text())
  9. }
  10. if err := scanner.Err(); err != nil {
  11. fmt.Println(err)
  12. }

huangapple
  • 本文由 发表于 2013年9月18日 08:33:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/18862033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定