Split a text file into two text files of equal size in order

func WordbyWordScan() { //A function for dividing a text file into two file texts.
    file, err := os.Open("file.txt.txt") //Open a file for doing operation on it.
    if err != nil {
        log.Fatal(err)
    }
    file1, err := os.Create("file1.txt.txt") //Create a file
    if err != nil {
        panic(err)
    }

    file2, err := os.Create("file2.txt.txt") //Create a file
    if err != nil {
        panic(err)
    }

    defer file.Close()                //close file at the end.
    defer file1.Close()               //close file at the end.
    defer file2.Close()               //close file at the end.
    file.Seek(0, 0)                   // "Seek sets the offset for the next Read or Write on file to offset."
    scanner := bufio.NewScanner(file) // "NewScanner returns a new Scanner to read from file"
    scanner.Split(bufio.ScanWords)    // "Set the split function for the scanning operation."
    w := 0
    for scanner.Scan() { //writing to file1 and file2
        var outfile *os.File
        if w%2 == 0 {
            outfile = file1
        } else {
            outfile = file2
        }
        fmt.Fprintln(outfile, scanner.Text()) //"Fprintln formats using the default formats for its operands and writes to outfile."
        w++
    }
    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }

}

This code above, split the file.txt word by word instead of splitting the first half of words and then the second half in order. For example, this sentence “Package bufio implements buffered I/O. It wraps an io.Reader or io.Writer object, creating another object.” should be split like this:

First half: “Package bufio implements buffered I/O. It wraps an io”

Second half: “.Reader or io.Writer object, creating another object.”

If anyone has any idea to fix this, I look forward to hearing from you.

What is the current, wrong output?

Hi,
Just copy and paste some text in the file.txt then run the code and you can see the results.
I look forward to hearing from you.

Why is it so difficult to ask a complete question? Tell us:

  • what do you do
  • what do you expect
  • what you get instead

I look forward to hearing from you.

for scanner.Scan() { //writing to file1 and file2
        var outfile *os.File
        if w%2 == 0 {
            outfile = file1
        } else {
            outfile = file2
        }
        fmt.Fprintln(outfile, scanner.Text()) //"Fprintln formats using the default formats for its operands and writes to outfile."
        w++
    }

This part of code above needs modification in order to get my satisfying result. I just want to split first half of words from the second half of words in order. Is that too difficult to understand? if you need more explanation, I will send you screenshots or file results.
I look forward to hearing from you.

Solution:

var s[]string 
        .
        .
        .
            
      for scanner.Scan() { // storing or appending file.txt string values to array s.

                    s = append(s, scanner.Text())

                }

                if err := scanner.Err(); err != nil {

                    log.Fatal(err)

                }

                //writing to file1 and file2

                if len(s)%2 == 0 { // if the occurences of words is an even number.

                    for i := 0; i <= len(s)/2-1; i++ { // Writing first half of words to file1

                        fmt.Fprintln(file1, s[i])

                    }

                    for j := len(s) / 2; j < len(s); j++ { // Writing second half of words to file2

                        fmt.Fprintln(file2, s[j])

                    }

                } else { // if the occurences of words is an odd number.

                    for i := 0; i <= len(s)/2; i++ { // Writing first part of words to file1

                        fmt.Fprintln(file1, s[i])

                    }

                    for j := len(s)/2 + 1; j < len(s); j++ { // Writing second part of words to file2

                        fmt.Fprintln(file2, s[j])

                    }

                }
        .
        .
        .