Skip to content

Commit

Permalink
Handle command line input and output file sepcification.
Browse files Browse the repository at this point in the history
  • Loading branch information
WingGithub committed Jun 28, 2024
1 parent 755ee2b commit 9746a1f
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions recsv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,25 @@ import (
)

func main() {
reader := csv.NewReader(os.Stdin)
inFile := os.Stdin
outFile := os.Stdout
argc := len(os.Args)
var err error
if argc > 1 {
inFile, err = os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer inFile.Close()
}
if argc > 2 {
outFile, err = os.Create(os.Args[2])
if err != nil {
log.Fatal(err)
}
defer outFile.Close()
}
reader := csv.NewReader(inFile)

// ReadAll reads all the records from the CSV file
// and Returns them as slice of slices of string
Expand All @@ -30,7 +48,7 @@ func main() {
return eq
})

w := csv.NewWriter(os.Stdout)
w := csv.NewWriter(outFile)
// Loop to iterate through
// and write out each of the string slice
for _, eachrecord := range records {
Expand Down

0 comments on commit 9746a1f

Please sign in to comment.