59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"strconv"
|
||
|
"bufio"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
// Create file to read
|
||
|
inputFile, err := os.Open("input.txt")
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
log.Fatal("Failed to create file.")
|
||
|
}
|
||
|
inputFileScanner := bufio.NewScanner(inputFile)
|
||
|
// For changing the buffer size
|
||
|
//const maxCapacity int =
|
||
|
//buf := make([]byte,maxCapacity)
|
||
|
//inputFileScanner.Buffer(buf, maxCapacity)
|
||
|
|
||
|
// Loop to create file handler array
|
||
|
var fileDescArray []*os.File
|
||
|
numberOfFileDesc := 7
|
||
|
for fileDescIndex := range numberOfFileDesc {
|
||
|
|
||
|
// Create and open file
|
||
|
handler, err := os.Create("file" + strconv.Itoa(fileDescIndex))
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// Append the handler to the list
|
||
|
fileDescArray = append(fileDescArray,handler)
|
||
|
}
|
||
|
|
||
|
// Loop through open file and write to file handlers
|
||
|
var fileArrayIndex int = 0
|
||
|
fileDescArrayLen := len(fileDescArray)
|
||
|
for inputFileScanner.Scan() {
|
||
|
(fileDescArray[fileArrayIndex % fileDescArrayLen]).Write(append(inputFileScanner.Bytes(),'\n'))
|
||
|
fileArrayIndex = fileArrayIndex + 1
|
||
|
}
|
||
|
|
||
|
// Loop to close all files
|
||
|
for _, handler := range fileDescArray {
|
||
|
if err := handler.Close(); err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
// Close input file
|
||
|
if err := inputFile.Close(); err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
|