0

In my code below, I'm hoping to read a text file filled with a number 1-5 per line. I want it to scan the line and read one of values and see if its < 6 and perform an action. Unfortunately I'm unable to take a slice of the values, they only print as an array. Any advice on how i can fix my code please?

//This is the part of the program that will read from a text file named "file"
//To see what numbers were selected last time so the recipe for week two can be
//a completely new recipe group of 5
f, err := os.Open("file")
if err != nil {
    fmt.Println(err)
}
for {
    var z int
    var n int
    n, err = fmt.Fscanln(f, &z)
    if n == 0 || err != nil {
        break
    }
    fmt.Println("int z :", z)
}
Tbalz
  • 662
  • 1
  • 7
  • 24

1 Answers1

2

Your code worked fine in my test. Likely the file is formatted slightly differently or is in a different location?

for {
    var z int
    var n int
    n, err = fmt.Fscan(f, &z)
    if n == 0 || err != nil {
        break
    }
    fmt.Println("int z :", z)
}

really, a variation of this question: Reading an integer from standard input in Golang

Community
  • 1
  • 1
foo
  • 701
  • 4
  • 8