0

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)
    }
}
James Henstridge
  • 42,244
  • 6
  • 132
  • 114
tjames68w
  • 381
  • 1
  • 4
  • 5
  • 2
    Can you provide some more information about the problem you're seeing? I downloaded and ran your program and it seems to behave correctly for the data I gave it. Even when I made sure my lines would exceed the length of the buffer. – James Henstridge Sep 18 '13 at 00:53
  • I have a text file of about 100 strings and only the last line of the text file shows on the console. I want essentially duplicate the text file to the screen. Instead only the last string shows. – tjames68w Sep 18 '13 at 01:17
  • What are the line ending characters in your file? You can use http://www.fileformat.info/tool/hexdump.htm to get a look into the actual file encoding and then add that to your question which will help with others testing it. – miltonb Sep 18 '13 at 01:18

1 Answers1

1

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: 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)
}
Community
  • 1
  • 1
miltonb
  • 6,905
  • 8
  • 45
  • 55
  • Are you sure about this? I adjusted the poster's program to add some extra debug output, and it definitely reads the file in lines. And the `isPrefix` part seems to work correctly when I adjusted the buffer to be shorter than the lines I had: it is set to true if a partial line is returned, rather than a line other than the final one. – James Henstridge Sep 18 '13 at 01:25
  • @JamesHenstridge, you are right, I had already edited the example when I added the debug. So it is reading line by line. Have edited my answer. – miltonb Sep 18 '13 at 01:28