duanlei8119 2017-03-23 14:13
浏览 343
已采纳

解析文件中的每一行之后,scanner.Text()的类型是什么?

I am currently reading a text file input.txt with the following inputs:

123
456
789

The code to parse it is:

package main

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

func main() {
    file, err := os.Open("input.txt")
    if err != nil {
        log.Fatal(err)
    }   
    defer file.Close()

    scanner := bufio.NewScanner(file)
    count := 0
    var line string
    for scanner.Scan() {
        count += 1
        line = scanner.Text()
        fmt.Println(line)
        if line == "123" {
            fmt.Println("EQUAL")
        }   
    }
}

Why does the first line of the file not match the hard coded string 123 in the code?

  • 写回答

1条回答 默认 最新

  • drqn5418 2017-03-23 16:09
    关注

    As mentioned in the comments, this is due to special characters in the file. In this case the utf8 bom, but could be dos format , or other non-printable characters.

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

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿