在go中比较两个文件,任何新行都被复制到一个新文件中

zy1mlcev  于 2022-12-16  发布在  Go
关注(0)|答案(1)|浏览(134)

我试图比较两个单独的文件充满了链接(约422行),并放置任何新的行/链接到第三个文件使用Golang。任何想法如何完成这一点?
我已经尝试了下面的代码来比较文档,但不断得到奇怪的结果(计数300新行时,它应该只有10),并没有存储任何新的链接,因为我不知道如何做到这一点。

// 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)
    }
}
2vuwiymt

2vuwiymt1#

@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()
}

相关问题