DAS_2024_1/bazunov_andrew_lab_2/SecondService/main.go
Bazunov Andrew Igorevich eeac04be49 Complete lab 2
2024-10-02 17:58:40 +04:00

80 lines
1.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.")
}