繁体   English   中英

比较 go 中的两个文件并将任何新行复制到新文件

[英]Comparing two files in go and any new lines get copied to a new file

我正在尝试比较两个充满链接(大约 422 行)的单独文档,并使用 Golang 将任何新行/链接放入第三个文档。 关于如何完成这个的任何想法?

我已经尝试使用下面的代码来比较文档,但结果一直很奇怪(当它应该只有 10 行时,却计算了 300 行新行)并且我没有存储任何新链接,因为我不确定如何这样做。


// code to compare documents
func Compare() {
    // open the first document
    file1, err := os.Open("Links_New.txt")
    if err != nil {
        fmt.Println("Error opening file:", err)
        return
    }
    defer file1.Close()

    // open the second document
    file2, err := os.Open("Links_Old.txt")
    if err != nil {
        fmt.Println("Error opening file:", err)
        return
    }
    defer file2.Close()

    // create scanners for both documents
    scanner1 := bufio.NewScanner(file1)
    scanner2 := bufio.NewScanner(file2)

    // initialize a counter for the number of New lines
    NewLines := 0

    // loop through each line in the first file
    for scanner1.Scan() {
        // check if the line exists in the second file
        if scanner2.Scan() {
            if scanner1.Text() != scanner2.Text() {
                NewLines++
            }
        } else {
            break
        }
    }

    // check for errors while scanning
    if err := scanner1.Err(); err != nil {
        fmt.Println("Error scanning file:", err)
        return
    }
    if err := scanner2.Err(); err != nil {
        fmt.Println("Error scanning file:", err)
        return
    }

    // print the number of new lines
    fmt.Println("\n\n Number of new lines:", NewLines)
}

// code to transfer links new to old
func ReadWrite() {
    // Open the source file for reading
    src, err := os.Open("Links_New.txt")
    if err != nil {
        panic(err)
    }
    defer src.Close()

    // Open the destination file for writing
    dst, err := os.Create("Links_Old.txt")
    if err != nil {
        panic(err)
    }
    defer dst.Close()

    // Copy the contents of the source file to the destination file
    _, err = io.Copy(dst, src)
    if err != nil {
        panic(err)
    }
}

@icza 是对的,使用 map

如果http://example.com/?a=1&b=2http://example.com/?b=2&a=1

下面是一个例子:

package main

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

var m = map[string]bool{}

func Compare() {
    // open the first document
    file1, _ := os.Open("Links_New.txt")
    defer file1.Close()

    // open the second document
    file2, _ := os.Open("Links_Old.txt")
    defer file2.Close()

    // create scanners for both documents
    r1 := bufio.NewReader(file1)

    // initialize a counter for the number of New lines
    NewLines := 0
    // loop through each line in the first file
    for {
        line, _, c := r1.ReadLine()
        m[string(line)] = true
        if c == io.EOF {
            break
        }
    }
    r2 := bufio.NewReader(file2)
    for {
        line, _, c := r2.ReadLine()
        if !m[string(line)] {
            NewLines++
            fmt.Printf("%s\n", line)
        }
        if c == io.EOF {
            break
        }
    }
    // print the number of new lines
    fmt.Println("\n\n Number of new lines:", NewLines)
}

func main() {
    Compare()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM