dongyi1748
2015-09-24 19:28 阅读 31
已采纳

Golang:在文件中跳过空白

In reading a file in Go I am attempting to skip all of the white spaces; however, I am having issues finding the correct method to do this. Any assistance would be appreciated

file, err := os.Open(filename) // For read access.
        this.file = file
        if err != nil {
                log.Fatal(err)
        }
//skip white space

  c := make([]byte, 1)

    char, err := this.file.Read(c)

    //skip white space
    for {
            //catch unintended errors
            if err != nil && err != io.EOF {
                    panic(err)
            }
            if err == io.EOF || !unicode.IsSpace(int(c)) {
                    break
            }
            //get next
            char, err := this.file.Read(c)
    }

I am just simply attempting to create a scanner for a file to read a single character at a time and ignore whitespace

EDIT

I changed a few things around to make use of bufio.Reader; however I have still fallen into issue What is the correct way to read a file character by character so that it might be compared to a specific symbol such as 'A' but also can ignore whitespace i.e unicode.isSpace(rune)

char, size, err := this.reader.ReadRune()
    //skip white space and comments
    for {
            //catch unintended errors
            if err != nil && err != io.EOF {
                    panic(err)
            }
            if err == io.EOF {
                    break
            }

            //skip it when their is no data or a space
            if size != 0 && char == '{' {
                    //Ignore Comments
                    //Documentation specifies no nested comments
                    for char != '}' {
                            char, size, err = this.reader.ReadRune()
                    }
            } else if !unicode.IsSpace(char) {
                    break
            }

            // Do something with the byte
            fmt.Print(char)

            //get next
            char, size, err = this.reader.ReadRune()
    }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douqian5553 douqian5553 2015-09-24 19:45

    Unless I'm misunderstanding your question, it would seem that you'd want a continue statement when encountering a space.

    c := make([]byte, 100)
    
    n, err := this.file.Read(c)
    
    //skip white space
    for {
        //catch unintended errors
        if err != nil && err != io.EOF {
            panic(err)
        }
        if err == io.EOF {
            break
        }
    
        for i := 0; i < n; i++ {
            ch := c[i]
    
            switch ch {
            case '{': // Do something
            case '}': // Do something else
            default:
                if unicode.IsSpace(int(ch)) {
                    continue
                }
                // Do whatever
            }
        }
    
            //get next
        n, err = this.file.Read(c)
    }
    

    I don't know why you're reading one byte at a time, but I left it that way in case it's intentional. At the very least, I'd think you'd want to read full unicode characters instead of individual bytes.

    点赞 评论 复制链接分享

相关推荐