package main

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"os"
	"strings"
)

func FileToStr(filename string) string {
	var txtlines string
	file, err := os.Open(filename)
	if err != nil {
		log.Panic(err)
	}
	scanner := bufio.NewScanner(file)
	scanner.Split(bufio.ScanLines)
	for scanner.Scan() {
		txtlines += scanner.Text()
	}
	file.Close()
	return txtlines
}

func convEscapeStr(str string) string {
	var newStr string
	newStr = str
	newStr = strings.ReplaceAll(newStr, "\r\n", "")
	newStr = strings.ReplaceAll(newStr, "\"", "\"\"")
	return newStr
}

func strToFile(str string, filename string) error {
	file, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer file.Close()
	_, err = io.WriteString(file, str)
	if err != nil {
		return err
	}
	return file.Sync()
}

func main() {
	fmt.Println("Hello, World!")
	text := FileToStr("test.txt")
	newText := convEscapeStr(text)
	err := strToFile(newText, "newtest.txt")
	if err != nil {
		log.Fatal(err)
	}
}
