Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

popraw ten kod o błąd: command-line-arguments .\ent1.go:85:15: undefined: strconv…

  1. User

    popraw ten kod o błąd:

    command-line-arguments

    .\ent1.go:85:15: undefined: strconv

    package main

    import ( "bufio" "fmt" "hash/crc32" "io/ioutil" "math/rand" "os" "strings" )

    func main() { reader := bufio.NewReader(os.Stdin)

    // Sprawdz, czy program jest uruchomiony w trybie kodowania (-e) lub dekodowania (-d) if len(os.Args) < 2 { fmt.Println("Podaj flage -e do kodowania lub -d do dekodowania.") return }

    mode := os.Args[1]

    switch mode { case "-e": // Wczytaj nazwe pliku do zakodowania fmt.Print("Podaj nazwe pliku do zakodowania: ") fileName, _ := reader.ReadString('\n') fileName = strings.TrimSpace(fileName)

    // Wczytaj zawartosc pliku fileData, err := ioutil.ReadFile(fileName) if err != nil { fmt.Println("Blad podczas wczytywania pliku:", err) return }

    // Zakoduj dane encodedData := encode(fileData)

    // Oblicz crc zakodowanych danych crc := crc32.ChecksumIEEE(encodedData)

    // Zapisz zakodowane dane do nowego pliku encodedFileName := fileName + ".encoded" err = ioutil.WriteFile(encodedFileName, encodedData, 0644) if err != nil { fmt.Println("Blad podczas zapisywania zakodowanego pliku:", err) return }

    // Zapisz crc do pliku crcFileName := fileName + ".crc" err = ioutil.WriteFile(crcFileName, []byte(fmt.Sprintf("%d", crc)), 0644) if err != nil { fmt.Println("Blad podczas zapisywania pliku crc:", err) return }

    fmt.Println("Plik zostal zakodowany i zapisany jako", encodedFileName) fmt.Println("CRC zakodowanych danych:", crc)

    case "-d": // Wczytaj nazwe pliku do dekodowania fmt.Print("Podaj nazwe pliku do dekodowania: ") decodedFileName, _ := reader.ReadString('\n') decodedFileName = strings.TrimSpace(decodedFileName)

    // Wczytaj zakodowane dane decodedData, err := ioutil.ReadFile(decodedFileName) if err != nil { fmt.Println("Blad podczas wczytywania pliku:", err) return }

    // Wczytaj crc crcFileName := decodedFileName + ".crc" crcData, err := ioutil.ReadFile(crcFileName) if err != nil { fmt.Println("Blad podczas wczytywania pliku crc:", err) return }

    // Sprawdz crc zakodowanych danych crc, err := strconv.ParseUint(strings.TrimSpace(string(crcData)), 10, 32) if err != nil { fmt.Println("Blad podczas parsowania crc:", err) return }

    calculatedCrc := crc32.ChecksumIEEE(decodedData) if crc != calculatedCrc { fmt.Println("Blad crc: spodziewane", crc, "otrzymane", calculatedCrc) return }

    // Odkoduj dane decodedData = decode(decodedData)

    // Zapisz odkodowane dane do nowego pliku decodedFileName = decodedFileName + ".decoded" err = ioutil.WriteFile(decodedFileName, decodedData, 0644) if err != nil { fmt.Println("Blad podczas zapisywania odkodowanego pliku:", err) return }

    fmt.Println("Plik zostal odkodowany i zapisany jako", decodedFileName)

    default: fmt.Println("Nieznana flaga. Podaj flage -e do kodowania lub -d do dekodowania.") } }

    func encode(data []byte) []byte { rand.Seed(42) key := make([]byte, len(data)) for i := range key { key[i] = byte(rand.Intn(256)) }

    encodedData := make([]byte, len(data)) for i := range data { encodedData[i] = data[i] ^ key[i] }

    encodedData = append(key, encodedData...)

    return encodedData }

    func decode(encodedData []byte) []byte { key := encodedData[:len(encodedData)/2] encodedData = encodedData[len(encodedData)/2:]

    decodedData := make([]byte, len(encodedData)) for i := range encodedData { decodedData[i] = encodedData[i] ^ key[i] }

    return decodedData }

  2. Assistant

    Aby rozwiązać ten błąd, musisz dodać import paczki strconv na początku pliku:

    import (
      "bufio"
      "fmt"
      "hash/crc32"
      "io/ioutil"
      "math/rand"
      "os"
      "strings"
      "strconv" // dodaj ten import
    )
    

    Dodatkowo, warto zauważyć, że funkcja strconv.ParseUint zwraca dwa wartości: wynik i błąd. Aby rozwiązać ten błąd, dodaj spójnik _ przed drugą wartością, aby ją zignorować:

    crc, _ := strconv.ParseUint(strings.TrimSpace(string(crcData)), 10, 32)
    

    Po wprowadzeniu tych zmian, kod powinien być poprawny.