Skip to content

Commit

Permalink
fix: edit the file reading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
RawanMostafa08 committed Sep 9, 2024
1 parent c9d6041 commit 02db807
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
11 changes: 6 additions & 5 deletions cmd/head/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"log"
"os"
"strings"

internal "github.com/codescalersinternships/Coreutils-Rawan-Mostafa/internal"
)
Expand All @@ -19,12 +18,14 @@ func main() {
if len(flag.Args()) == 0 {
log.Fatal("No file argument passed to the command")
}
data, err := os.ReadFile(flag.Args()[0])

filePath := flag.Args()[0]
file, err := os.Open(filePath)
if err != nil {
log.Fatal("Error in reading the file")
log.Fatal("Error in opening the file")
}

scanner := bufio.NewScanner(strings.NewReader(string(data)))
defer file.Close()
scanner := bufio.NewScanner(file)

err = internal.Head(scanner, n)
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions cmd/tail/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"log"
"os"
"strings"

internal "github.com/codescalersinternships/Coreutils-Rawan-Mostafa/internal"
)
Expand All @@ -19,12 +18,14 @@ func main() {
if len(flag.Args()) == 0 {
log.Fatal("No file argument passed to the command")
}
data, err := os.ReadFile(flag.Args()[0])

filePath := flag.Args()[0]
file, err := os.Open(filePath)
if err != nil {
log.Fatal("Error in reading the file")
log.Fatal("Error in opening the file")
}

scanner := bufio.NewScanner(strings.NewReader(string(data)))
defer file.Close()
scanner := bufio.NewScanner(file)

err = internal.Tail(scanner, n)
if err != nil {
Expand Down

0 comments on commit 02db807

Please sign in to comment.