Skip to content

Commit

Permalink
scan using os package
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangbits committed May 27, 2020
1 parent cd632fb commit 5ef3676
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 15 deletions.
12 changes: 5 additions & 7 deletions c1-m4-activity/names.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
hoang le
minh tran
nghia nguyen
tuan dang
hung tran
khanh tran
tam do
huu dao
van ly
nam ho
nam ho
hng le
nghia nguyen
tuan dang
hung tran
98 changes: 90 additions & 8 deletions c1-m4-activity/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strings"
)

const (
maxLength = 20
maxLength = 20
defaultScanRange = 2
defaultReadOffset = 2
)

// Name represents data in each line from text
Expand All @@ -19,8 +23,8 @@ type Name struct {

// InitMe init property for Name
func (n *Name) InitMe(fname, lname string) {
n.fname = fname
n.lname = lname
n.fname = strings.Trim(fname, "\n")
n.lname = strings.Trim(lname, "\n")
var maxRunes []rune
if len(fname) > maxLength {
maxRunes = []rune(fname)
Expand All @@ -40,9 +44,18 @@ func (n *Name) InitMe(fname, lname string) {
*/

func main() {
var fileName string
fmt.Println("Enter name of the file:")
fmt.Scan(&fileName)
// var fileName string
// fmt.Println("Enter name of the file:")
// fmt.Scan(&fileName)

names := readUsingOs("names.txt")
fmt.Printf("--------")
fmt.Printf("names value: %v\n", names)
// fmt.Printf("names value 12: %v\n", names[12])
// fmt.Printf("length: %d,capacity: %d \n", len(names), cap(names))
}

func readUsingBuf(fileName string) []Name {
file, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
Expand All @@ -68,6 +81,75 @@ func main() {
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
fmt.Printf("names value: %v\n", names)
fmt.Printf("length: %d,capacity: %d \n", len(names), cap(names))
return names
}

func readUsingOs(fileName string) []Name {
file, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
defer file.Close()
names := make([]Name, 0, 20)

var scanRange int = 4
var readOffset int64 = 0
var foundNewLine bool = false
var meetEOF bool = false

text := make([]byte, scanRange)
count, err := file.Read(text)
var cycle int = 0
for count != 0 {
cycle++
// fmt.Printf("~~~~~~~~readOffset: %v~~~~scanRange: %v \n", readOffset, scanRange)
// fmt.Printf("text before check newline and whitespace:%v-with len:%v\n", string(text), len(text))
foundNewLine = false
for pos, char := range text {
// fmt.Printf("------POS: %v char: %c", pos, char)
if (char == '\n' || meetEOF) && pos != 0 {
foundNewLine = true
scanRange = defaultScanRange
// fmt.Printf("found '\\n' readOffset %v add pos %v in text have '\\n'\n", readOffset, pos)
line := text[:pos]
if meetEOF {
fmt.Printf("Count %v\n", count)
line = text[:count]
readOffset += int64(count)
} else {
readOffset += int64(pos)
}
fmt.Printf("found '\\n' line: %v and pos: %v\n", string(line), pos)
for pos, char := range line {
if char == ' ' {
tempName := &Name{}
fmt.Printf("ADDING fname %v \n", string(line[:pos]))
fmt.Printf("ADDING lname %v \n", string(line[pos+1:]))
tempName.InitMe(string(line[:pos]), string(line[pos+1:]))
names = append(names, *tempName)
// only check first new line
break
}
}
break
}
}
fmt.Println("ReadAtReadAtReadAtReadAtReadAtReadAt")
if !foundNewLine {
scanRange *= 2
// fmt.Printf("NF scanRange %v count: %v readOffset %v\n", scanRange, count, readOffset)
}
text = make([]byte, scanRange)
count, err = file.ReadAt(text, readOffset)
// fmt.Printf("Error:%v-Count:%v\n", err, count)
if err == io.EOF && meetEOF {
break
}
if err == io.EOF {
meetEOF = true
text = make([]byte, count)
count, err = file.ReadAt(text, readOffset)
}
}
return names
}

0 comments on commit 5ef3676

Please sign in to comment.