DAS_2024_1/bazunov_andrew_lab_2/SecondService/main.go

80 lines
1.4 KiB
Go
Raw Normal View History

2024-10-02 17:58:40 +04:00
package main
import (
"bufio"
"fmt"
"os"
)
//Ищет наименьшее число из файла /var/data/data.txt и сохраняет его третью степень в /var/result/result.txt.
const INPUT = "/var/data/data.txt"
const OUTPUT = "/var/result/result.txt"
func ReadlinesFromFile(filename string) ([]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
var output []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
output = append(output, scanner.Text())
}
err = file.Close()
if err != nil {
return nil, err
}
return output, nil
}
func WriteIntToFile(filename string, i int) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
}
}(file)
_, err = file.WriteString(fmt.Sprintf("%d\n", i))
if err != nil {
return err
}
return nil
}
func main() {
lines, err := ReadlinesFromFile(INPUT)
if err != nil {
fmt.Println(err)
}
minValue := 0
for _, line := range lines {
if intValue, err := fmt.Sscanf(line, "%d", &minValue); err != nil {
fmt.Println(err)
} else {
if minValue >= intValue {
minValue = intValue
}
}
}
if err = WriteIntToFile(OUTPUT, minValue); err != nil {
return
} else {
fmt.Printf("Write %d to %s\n", minValue, OUTPUT)
}
fmt.Println("Second Service is end.")
}