-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileOperations.go
More file actions
64 lines (54 loc) · 1.18 KB
/
fileOperations.go
File metadata and controls
64 lines (54 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func readFile(filePath string) ([]string, error) {
var s []string
file, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
if err != nil {
if err.Error() == "EOF" {
break
}
fmt.Println("error reading file: ", err)
return nil, err
}
s = append(s, line)
}
return s, nil
}
func saveToFile(filePath string, data []string) {
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
log.Println("error opening file:", err)
return
}
// Ensure the file is closed after the function completes
defer file.Close()
// Create a buffered writer
writer := bufio.NewWriter(file)
// Write a string to the buffer
for _, line := range data {
_, err = writer.WriteString(line)
if err != nil {
fmt.Println("error writing to buffer:", err)
return
}
}
// Flush the buffer to ensure all data is written to the file
err = writer.Flush()
if err != nil {
fmt.Println("error flushing buffer:", err)
return
}
fmt.Println("converted SQL is saved to " + filePath)
}