Skip to content

Commit 50c2232

Browse files
committed
Refactor input.dat and implement file writing in main.go; add readcsv.go for CSV file handling and gob serialization in gob/main.go
1 parent 0c5e34c commit 50c2232

File tree

4 files changed

+119
-2
lines changed

4 files changed

+119
-2
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"io"
7+
"log"
8+
"os"
9+
"strconv"
10+
"strings"
11+
)
12+
13+
type Book struct {
14+
title string
15+
price float64
16+
quantity int
17+
}
18+
19+
func main() {
20+
bks := make([]Book, 1)
21+
file, err := os.Open("products.txt")
22+
if err != nil {
23+
log.Fatalf("Error %s opening file products.txt: ", err)
24+
}
25+
defer file.Close()
26+
27+
reader := bufio.NewReader(file)
28+
for {
29+
// read one line from the file:
30+
line, err := reader.ReadString('\n')
31+
if err == io.EOF {
32+
break
33+
}
34+
// remove \r and \n so 2 in Windows, in Linux only \n, so 1:
35+
line = string(line[:len(line)-2])
36+
//fmt.Printf("The input was: -%s-", line)
37+
38+
strSl := strings.Split(line, ";")
39+
book := new(Book)
40+
book.title = strSl[0]
41+
book.price, err = strconv.ParseFloat(strSl[1], 32)
42+
if err != nil {
43+
fmt.Printf("Error in file: %v", err)
44+
}
45+
//fmt.Printf("The quan was:-%s-", strSl[2])
46+
book.quantity, err = strconv.Atoi(strSl[2])
47+
if err != nil {
48+
fmt.Printf("Error in file: %v", err)
49+
}
50+
if bks[0].title == "" {
51+
bks[0] = *book
52+
} else {
53+
bks = append(bks, *book)
54+
}
55+
}
56+
fmt.Println("We have read the following books from the file: ")
57+
for _, bk := range bks {
58+
fmt.Println(bk)
59+
}
60+
}

Reading_writing/gob/main.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/gob"
6+
"fmt"
7+
)
8+
9+
// Define a sample struct
10+
type User struct {
11+
Name string
12+
Age int
13+
}
14+
15+
func main() {
16+
// Original data
17+
original := User{Name: "Alice", Age: 30}
18+
19+
// Create a buffer to hold the encoded data
20+
var buf bytes.Buffer
21+
22+
// Create a gob encoder writing to the buffer
23+
enc := gob.NewEncoder(&buf)
24+
25+
// Encode the struct into the buffer
26+
err := enc.Encode(original)
27+
if err != nil {
28+
fmt.Println("Encoding failed:", err)
29+
return
30+
}
31+
32+
fmt.Println("✅ Serialized data into buffer.")
33+
34+
// Create a decoder reading from the same buffer
35+
dec := gob.NewDecoder(&buf)
36+
37+
// Create a variable to hold the decoded data
38+
var decoded User
39+
40+
// Decode into it
41+
err = dec.Decode(&decoded)
42+
if err != nil {
43+
fmt.Println("Decoding failed:", err)
44+
return
45+
}
46+
47+
fmt.Println("✅ Decoded data from buffer:")
48+
fmt.Println("Name:", decoded.Name)
49+
fmt.Println("Age:", decoded.Age)
50+
}

Reading_writing/input.dat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Hello sithu win
2-
win sithu sithu
1+
hello, world in a file
2+
hu sithu
33
my name hello
44
myat su yin

Reading_writing/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ func main() {
5252
fmt.Println(col2)
5353
fmt.Println(col3)
5454

55+
//writing into files
56+
os.Stdout.WriteString("hello, world\n")
57+
f, _ := os.OpenFile("input.dat", os.O_CREATE|os.O_WRONLY, 0)
58+
defer f.Close()
59+
60+
f.WriteString("hello, world in a file\n")
61+
5562
for {
5663
inputString, readerError := inputReader.ReadString('\n')
5764
if readerError == io.EOF {

0 commit comments

Comments
 (0)