FAQ
I am trying to write a function that reads a slice of integers, such as
"[1,5,3,2,0]", from a text file. Here is the code:

func readSlice(reader *os.File) []int {
slice := []int{}
_,err := fmt.Fscanf(reader, "[");
if (err != nil) {
panic("Slice should start with '['!")
}

var el int
for {
_,err := fmt.Fscanf(reader, "%d", &el);
if err != nil {
break;
}
slice = append(slice, el)
}
_,err = fmt.Fscanf(reader, "]");
if (err != nil) {
panic("Slice should end with ']'!")
}
return slice
}

Contrary to my expectations, when Fscanf encounters "]" instead of an
integer, it does not push "]" back (as fscanf() would have done in C), so
the program panics.
Is there an easy fix or work-around for this problem?

Thank you!
Meir

Search Discussions

  • Meir at Sep 6, 2012 at 11:36 am
    May be I can ask more to the point: how do I get to know what was the
    character that did not match the pattern when Fscanf() has already
    swallowed it?
    On Thursday, September 6, 2012 2:11:25 PM UTC+3, Meir wrote:


    I am trying to write a function that reads a slice of integers, such as
    "[1,5,3,2,0]", from a text file. Here is the code:

    func readSlice(reader *os.File) []int {
    slice := []int{}
    _,err := fmt.Fscanf(reader, "[");
    if (err != nil) {
    panic("Slice should start with '['!")
    }

    var el int
    for {
    _,err := fmt.Fscanf(reader, "%d", &el);
    if err != nil {
    break;
    }
    slice = append(slice, el)
    }
    _,err = fmt.Fscanf(reader, "]");
    if (err != nil) {
    panic("Slice should end with ']'!")
    }
    return slice
    }

    Contrary to my expectations, when Fscanf encounters "]" instead of an
    integer, it does not push "]" back (as fscanf() would have done in C), so
    the program panics.
    Is there an easy fix or work-around for this problem?

    Thank you!
    Meir
  • Christoph Hack at Sep 6, 2012 at 12:06 pm

    On Thursday, September 6, 2012 1:36:02 PM UTC+2, Meir wrote:
    May be I can ask more to the point: how do I get to know what was the
    character that did not match the pattern when Fscanf() has already
    swallowed it?

    *os.File is completely unbuffered. Wrap it in a *bufio.Reader and fmt will
    thankfully use its UnreadByte() / UnreadRune() method to push back the last
    character again.
  • Roger peppe at Sep 6, 2012 at 12:32 pm
    what christoph says: http://play.golang.org/p/TTdpfmK6NE
    On 6 September 2012 13:06, Christoph Hack wrote:
    On Thursday, September 6, 2012 1:36:02 PM UTC+2, Meir wrote:

    May be I can ask more to the point: how do I get to know what was the
    character that did not match the pattern when Fscanf() has already swallowed
    it?

    *os.File is completely unbuffered. Wrap it in a *bufio.Reader and fmt will
    thankfully use its UnreadByte() / UnreadRune() method to push back the last
    character again.
  • Meir Goldenberg at Sep 6, 2012 at 2:20 pm
    Great! Thanks!
    On Thu, Sep 6, 2012 at 3:10 PM, roger peppe wrote:

    what christoph says: http://play.golang.org/p/TTdpfmK6NE
    On 6 September 2012 13:06, Christoph Hack wrote:
    On Thursday, September 6, 2012 1:36:02 PM UTC+2, Meir wrote:

    May be I can ask more to the point: how do I get to know what was the
    character that did not match the pattern when Fscanf() has already
    swallowed
    it?

    *os.File is completely unbuffered. Wrap it in a *bufio.Reader and fmt will
    thankfully use its UnreadByte() / UnreadRune() method to push back the last
    character again.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 6, '12 at 11:11a
activeSep 6, '12 at 2:20p
posts5
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase