donglu9134 2016-12-28 12:16
浏览 1136
已采纳

用Golang替换文件中的一行

I'm new to Golang, starting out with some examples. Currently, what I'm trying to do is reading a file line by line and replace it with another string in case it meets a certain condition. The file is use for testing purposes contains four lines:

one
two
three
four

The code working on that file looks like this:

 func main() {

     file, err := os.OpenFile("test.txt", os.O_RDWR, 0666)

     if err != nil {
       panic(err)
     }

     reader := bufio.NewReader(file)

      for {

         fmt.Print("Try to read ...
")

         pos,_ := file.Seek(0, 1)
         log.Printf("Position in file is: %d", pos)
         bytes, _, _ := reader.ReadLine()

         if (len(bytes) == 0) {
            break
         }

         lineString := string(bytes)

         if(lineString == "two") {
             file.Seek(int64(-(len(lineString))), 1)
             file.WriteString("This is a test.")
         }

         fmt.Printf(lineString + "
")
   }

    file.Close()
 }

As you can see in the code snippet, I want to replace the string "two" with "This is a test" as soon as this string is read from the file. In order to get the current position within the file I use Go's Seek method. However, what happens is that always the last line gets replaced by This is a test, making the file looking like this:

one
two
three
This is a test

Examining the output of the print statement which writes the current file position to the terminal, I get that kind of output after the first line has been read:

2016/12/28 21:10:31 Try to read ...
2016/12/28 21:10:31 Position in file is: 19

So after the first read, the position cursor already points to the end of my file, which explains why the new string gets appended to the end. Does anyone understand what is happening here or rather what is causing that behavior?

展开全部

  • 写回答

1条回答 默认 最新

  • dran0703 2016-12-28 12:23
    关注

    The Reader is not controller by the file.Seek. You have declared the reader as: reader := bufio.NewReader(file) and then you read one line at a time bytes, _, _ := reader.ReadLine() however the file.Seek does not change the position that the reader is reading.

    Suggest you read about the ReadSeeker in the docs and switch over to using that. Also there is an example using the SectionReader.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部