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.

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

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境