普通网友 2013-01-22 22:49
浏览 69

从文件读取时如何查找EOF

I am using the following code to read a file in Go:

spoon , err := ioutil.ReadFile(os.Args[1])
if err!=nil {
        panic ("File reading error")
}

Now I check for every byte I pick for what character it is. For example:

spoon[i]==' ' //for checking space

Likewise I read the whole file (I know there maybe other ways of reading it) but keeping this way intact, how can I know that I have reached EOF of the file and I should stop reading it further?

Please don't suggest to find the length of spoon and start a loop. I want a sure shot way of finding EOF.

  • 写回答

4条回答 默认 最新

  • doumubi6784 2013-01-22 22:56
    关注

    A slice has no concept of end of file. The slice returned by ioutil.ReadFile has a specific length, which reflects the size of the file it was read from. A common idiom, but only one of the possible used in this case, is to range the slice, effectively "consuming" all of the bytes, originally sitting in the file:

    for i, b := range spoon {
            // At index 'i' is byte 'b'
            // At file's offset 'i', 'b' was read
            ... do something useful here
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)