From 5ef3676040bbc79ac7ba349b1e6264521cf3e4fe Mon Sep 17 00:00:00 2001 From: hoang Date: Wed, 27 May 2020 11:36:08 +0700 Subject: [PATCH] scan using os package --- c1-m4-activity/names.txt | 12 ++--- c1-m4-activity/read.go | 98 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 95 insertions(+), 15 deletions(-) diff --git a/c1-m4-activity/names.txt b/c1-m4-activity/names.txt index ba2b472..a819c00 100644 --- a/c1-m4-activity/names.txt +++ b/c1-m4-activity/names.txt @@ -1,10 +1,8 @@ -hoang le -minh tran -nghia nguyen -tuan dang -hung tran khanh tran -tam do huu dao van ly -nam ho \ No newline at end of file +nam ho +hng le +nghia nguyen +tuan dang +hung tran diff --git a/c1-m4-activity/read.go b/c1-m4-activity/read.go index 9062284..000cf55 100644 --- a/c1-m4-activity/read.go +++ b/c1-m4-activity/read.go @@ -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 @@ -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) @@ -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) @@ -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 }