doujie3888 2016-08-01 05:50
浏览 169
已采纳

从Golang文件中读取特定字符

I want to read a text file character by character and print invalid input for those characters that exceed "H"and numbers that exceed "8". eg: my input is

I9,A2
A10,C3
D2,L3

output: invalid input for all three

package main

import (
    "bufio"
    "fmt"
    "log"
    "os"
)

func readLines(path string) ([]string, error) {
    file, err := os.Open(path)
    if err != nil {
        return nil, err
    }
    defer file.Close()
    var lines []string
    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        lines = append(lines, scanner.Text())
    }
    return lines, scanner.Err()
}

func main() {
    lines, err := readLines("chessin.txt")
    if err != nil {
        log.Fatalf("readLines: %s", err)
    }
    var numLines int = len(lines)
    for i := 0; i < numLines; i++ {
        for j := 0; j < len(lines[i]); j++ {
            if j > 'H' {
                fmt.Printf("invalid input")
            }
        }
    }
}
  • 写回答

2条回答 默认 最新

  • douliang1900 2016-08-01 07:15
    关注

    You need to edit the inner loop to check for every input line,
    and find the number then comma and so, like this working sample code:

    package main
    
    import (
        "bufio"
        "fmt"
        "log"
        "os"
        "strconv"
        "strings"
    )
    
    func readLines(path string) ([]string, error) {
        file, err := os.Open(path)
        if err != nil {
            return nil, err
        }
        defer file.Close()
        var lines []string
        scanner := bufio.NewScanner(file)
        for scanner.Scan() {
            lines = append(lines, scanner.Text())
        }
        return lines, scanner.Err()
    }
    func checkOne(letter byte, number, i, j int) {
        if letter > 'H' {
            fmt.Printf("invalid input %q @ (%d, %d) 
    ", letter, i, j)
        }
        if number > 8 {
            fmt.Printf("invalid input number %d @ (%d, %d) 
    ", number, i, j+1)
    
        }
    }
    func main() {
        lines, err := readLines("chessin.txt")
        if err != nil {
            log.Fatalf("readLines: %s", err)
        }
        var numLines int = len(lines)
        for i := 0; i < numLines; i++ {
            line := lines[i]
            j := 0
            comma := strings.IndexByte(line, ',')
            if comma == -1 {
                log.Fatalf("comma not found at line: %d", i)
            }
            number, err := strconv.Atoi(line[j+1 : comma])
            if err != nil {
                log.Fatalf("line:%d err: %s", i, err)
            }
            checkOne(line[j], number, i, j)
    
            j = comma + 1
            number, err = strconv.Atoi(line[j+1:])
            if err != nil {
                log.Fatalf("line:%d err: %s", i, err)
            }
            checkOne(line[j], number, i, j)
        }
    }
    

    input file "chessin.txt":

    I9,A2
    A10,C3
    D2,L3
    

    output:

    invalid input 'I' @ (0, 0) 
    invalid input number 9 @ (0, 1) 
    invalid input number 10 @ (1, 1) 
    invalid input 'L' @ (2, 3) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 商城生产日期批次库存问题
  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle