dongyi1748 2015-09-24 19:28
浏览 34
已采纳

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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向